Reputation: 181
I am trying to run a MERN application to validate what it has, but it is sending me this error in several files.
Error: Type 'CatalogType' does not satisfy the constraint 'Document'. Type 'CatalogType' is missing the following properties from type 'Document': $ignore, $isDefault, $isDeleted, $isEmpty, and 45 more.ts(2344)
My code:
const {
Types: { ObjectId },
} = Schema;
export interface CatalogType {
_id: any;
code: string;
description: string;
version: number;
}
export interface CatalogDocumentType extends Document,CatalogType{
_id: number;
}
export const schema = new Schema<CatalogType>(
{
id: {
type: ObjectId,
},
code: {
type: String,
required: true,
},
description: {
type: String,
required: false,
},
version: {
type: Number,
required: false,
},
},
{
timestamps: {
currentTime: () => new TimeZone().getLocaleCSTfromGMT(),
},
},
);
export const Model = model<CatalogDocumentType>('Catalog', schema);
Help me, please!
Upvotes: 0
Views: 2686
Reputation: 102207
The generic parameters of generic class Schema
has generic constraint, see index.d.ts.
class Schema<DocType extends Document = Document, M extends Model<DocType> = Model<DocType>> extends events.EventEmitter {/**..*/}
The generic parameter DocType
Must meet Document
type constraints. So the correct way is:
import { Schema, model, Document, Model } from 'mongoose';
const {
Types: { ObjectId },
} = Schema;
export interface CatalogTypeDocument extends Document {
_id: number;
code: string;
description: string;
version: number;
}
export interface CatalogTypeModel extends Model<CatalogTypeDocument> {}
export const schema = new Schema<CatalogTypeDocument>({
id: {
type: ObjectId,
},
code: {
type: String,
required: true,
},
description: {
type: String,
required: false,
},
version: {
type: Number,
required: false,
},
});
export const CatalogTypeModel = model<CatalogTypeDocument, CatalogTypeModel>('Catalog', schema);
For more info, see mongoosejs-typescript-docs
package versions:
"mongoose": "^5.11.9",
"@types/mongoose": "^5.10.3",
"typescript": "^3.7.2"
Upvotes: 4