You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constExampleSchema=newSchema({names: [{type: String,},],},{methods: {addToNames: asyncfunction(name: string){// this.updateOne() // accessiblethis.names.addToSet(name);// Property 'addToSet' does not exist on type 'string[]'. [2339]},},},);
this object in a methods is a hydrated document, however array deduced not as mongoose array but as plain array instead. Example demonstrates this issue by trying to use addToSet method of a mongoose array, but getting type error.
Same issue can be depicted with this example:
typeIExampleDocument=HydratedDocumentFromSchema<typeofExampleSchema>;constExample=model<IExampleDocument>("example",ExampleSchema);constexample=awaitExample.create({names: ["hello"]});example.names.addToSet("example");// Property 'addToSet' does not exist on type 'string[]'. [2339]
I have pretty vague understanding of how schemas in typescript should be properly defined, I've read that we can provide interfaces ourselves by using template parameters of model and Schema however it essentially produces a duplication in a boilerplate types and defies already defined principles of deduction established with HydratedDocumentFromSchema, InferSchemaType and other typescript helper functions.
The text was updated successfully, but these errors were encountered:
Prerequisites
Mongoose version
8.6.2
Node.js version
20.18.0
MongoDB version
7.0.14
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
10.0.22631
Issue
Here is simple example of schema in mongoose:
this
object in a methods is a hydrated document, however array deduced not as mongoose array but as plain array instead. Example demonstrates this issue by trying to useaddToSet
method of a mongoose array, but getting type error.Same issue can be depicted with this example:
I have pretty vague understanding of how schemas in typescript should be properly defined, I've read that we can provide interfaces ourselves by using template parameters of
model
andSchema
however it essentially produces a duplication in a boilerplate types and defies already defined principles of deduction established withHydratedDocumentFromSchema
,InferSchemaType
and other typescript helper functions.The text was updated successfully, but these errors were encountered: