francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
103 stars 24 forks source link

Interfaces with a mongoose.Types.ObjectId causes an error #107

Closed florianrubel closed 1 year ago

florianrubel commented 1 year ago

I have the following schema

const externalExportServerSchema = new mongoose.Schema<CompanyExternalExportServerList>({
    ....
    exportKey: {
        type: mongoose.Types.ObjectId,
        required: false,
        default: null,
    },
})

This is a part of the generated interface

export type CompanyExternalExportServerList = {
    ...
    exportKey?: mongoose.Types.ObjectId
}

And this leads to this error

(property) exportKey?: mongoose.SchemaDefinitionProperty<mongoose.Types.ObjectId>
Type '{ type: typeof mongoose.Types.ObjectId; required: false; default: null; }' is not assignable to type 'ObjectId | SchemaDefinitionProperty<ObjectId>'.
  Types of property 'type' are incompatible.
    Type 'typeof ObjectId' is not assignable to type 'typeof Mixed | ObjectIdSchemaDefinition'.
      Type 'typeof ObjectId' is missing the following properties from type 'typeof Mixed': schemaName, cast, checkRequired, set, getts(2322)
Company.model.ts(142, 5): The expected type comes from property 'exportKey' which is declared here on type 'CompanyExternalExportServerList | { text?: SchemaDefinitionProperty<string>; exportType?: SchemaDefinitionProperty<string>; ... 11 more ...; _id?: SchemaDefinitionProperty<...>; }'

Any idea to fix this?

florianrubel commented 1 year ago

Got the solution:

const externalExportServerSchema = new mongoose.Schema<CompanyExternalExportServerList>({
    ....
    exportKey: {
        type: mongoose.Schema.Types.ObjectId,
        required: false,
        default: null,
    },
})