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
The following script doesn't compile, and errors out on the line where we Shop.find, complaining about the filter type.
importmongoosefrom'mongoose';const{ Schema }=mongoose;constshopSchema=newSchema<IShop>({brandId: {type: Schema.Types.ObjectId,ref: 'Business'}});constShop=mongoose.model<IShop>('Shop',shopSchema);run().catch(console.error);asyncfunctionrun(){// passes if we change it to: { $in: [new mongoose.Types.ObjectId('0'.repeat(24))] }awaitShop.find({brandId: {$in: ['0'.repeat(24)]}});console.log('Done.');}interfaceIShop{brandId: mongoose.Types.ObjectId}
The error:
No overload matches this call.
Overload 1 of 3, '(callback?: Callback<(Document<any, any, IShop> & IShop & { _id: ObjectId; })[]>): Query<(Document<any, any, IShop> & IShop & { ...; })[], Document<...> & ... 1 more ... & { ...; }, {}, IShop>', gave the following error.
Argument of type '{ brandId: { $in: string[]; }; }' is not assignable to parameter of type 'Callback<(Document<any, any, IShop> & IShop & { _id: ObjectId; })[]>'.
Object literal may only specify known properties, and 'brandId' does not exist in type 'Callback<(Document<any, any, IShop> & IShop & { _id: ObjectId; })[]>'.
Overload 2 of 3, '(filter: FilterQuery<IShop>, callback?: Callback<IShop[]>): Query<(Document<any, any, IShop> & IShop & { _id: ObjectId; })[], Document<...> & ... 1 more ... & { ...; }, {}, IShop>', gave the following error.
Type 'string' is not assignable to type 'ObjectId'.
Overload 3 of 3, '(filter: FilterQuery<IShop>, projection?: any, options?: QueryOptions, callback?: Callback<(Document<any, any, IShop> & IShop & { ...; })[]>): Query<...>', gave the following error.
Type 'string' is not assignable to type 'ObjectId'.ts(2769)
This may be expected behavior since we're passing stringified values of object ids, but then again in JS, mongoose is nice enough to cast those ids out of the box for us, but that's not the case for TypeScript.
Since casting is one of the main selling points of mongoose, I think we should include the same behavior in Typescript.
Quoting the mongoose homepage:
writing MongoDB validation, casting and business logic boilerplate is a drag.
functionfindById(companyId: string){returnCompany.findById(companyId)// No complains finding ObjectIds using a string}functionfindByStringId(companyId: string){returnCompany.find({_id: companyId})// No complains finding ObjectIds using a string}functionfindByIds(companyIds: string[]){returnCompany.find({_id: {$in: companyIds}})// No overload matches this call... Type 'string[]' is not assignable to type 'ObjectId[]'.}
If this was intended, I think the first two examples would be invalid as well?
The following script doesn't compile, and errors out on the line where we
Shop.find
, complaining about the filter type.The error:
This may be expected behavior since we're passing stringified values of object ids, but then again in JS, mongoose is nice enough to cast those ids out of the box for us, but that's not the case for TypeScript.
Since casting is one of the main selling points of mongoose, I think we should include the same behavior in Typescript.
Quoting the mongoose homepage:
What do you think?
@vkarpov15 @taxilian @IslandRhythms
P.S: this used to work with mongoose 5.x.
The text was updated successfully, but these errors were encountered: