Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
Issue with Date and Mongoose Typescript

I'm facing the issue with the Mongoose official document founded here.

import { Schema, Model, model } from 'mongoose';

export interface IUser {
  name: string;
  email: string;
  avatar?: string;
  created: Date;
}

const schema = new Schema<IUser, Model<IUser>, IUser>({
  name: { type: String, required: true },
  email: String,
  avatar: String,
  created: { type: Date, default: Date.now },
});

export const UserModel = model<IUser>('User', schema);

My problem is the created type in IUser is not the same as in schema and got the error:

Type '{ type: DateConstructor; default: () => number; }' is not assignable to type 'typeof SchemaType | Schema<any, any, undefined, unknown> | Schema<any, any, undefined, unknown>[] | readonly Schema<any, any, undefined, unknown>[] | Function[] | ... 6 more ... | undefined'.
  Types of property 'type' are incompatible.
    Type 'DateConstructor' is not assignable to type 'Date | typeof SchemaType | Schema<any, any, undefined, unknown> | undefined'.
      Type 'DateConstructor' is missing the following properties from type 'typeof SchemaType': cast, checkRequired, set, getts(2322)
(property) created?: typeof SchemaType | Schema<any, any, undefined, unknown> | Schema<any, any, undefined, unknown>[] | readonly Schema<any, any, undefined, unknown>[] | Function[] | ... 6 more ... | undefined

Please let me know how to fix this.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Date.now() is a function which returns number. Instead of it, try using new Date() only. Also need to make changes in the type of createdAt to Number.

In the given doc link, createdAt field type is number but here you have written Date.

interface User {
  name: string;
  email: string;
  avatar?: string;
  createdAt: number;
}

OR

createdAt and updatedAt are timestamps which can be used directly without specifying in the schema.

const schema = new Schema<IUser, Model<IUser>, IUser>({
  name: { type: String, required: true },
  email: String,
  avatar: String
},{
    timestamps: true
});
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda