francescov1 / mongoose-tsgen

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

Regarding the type when setting an alias. #122

Closed yuuri111 closed 1 year ago

yuuri111 commented 1 year ago

Hello. I am having issues trying to set an alias.

"node":"16.19"
"npm": "^9.5.0",
"mongoose": "^6.7.2",
"mongoose-tsgen": "^9.1.0"

When setting an alias, I believe the type becomes 'any'. Is there a way to set the type to the one specified by 'type'?

ex) models/test.ts

interface TestSchemaFields extends Document {
  _id: string
  objectId: string
}

const TestSchema = new Schema<TestSchemaFields>({
  _id: {
    type: String,
    required: true,
    unique: true,
    alias: 'objectId',
  },
})

mongoose-types.ts

export type TestDocument = mongoose.Document<string, TestQueries> &
  TestMethods & {
    _id: string
    objectId: any
  }
francescov1 commented 1 year ago

Hey @yuuri111, thanks for submitting. The library doesn't currently have any handling for aliased fields, which is why its being typed as any. I dont think it should be too hard to add, will keep you updated!

francescov1 commented 1 year ago

Fix is live in v9.2.6 🚀