[mongoose]: allow numbers to be typed corrently when used as _id (#43760)

Co-authored-by: Jonathan Burke <jonathan.burke@codecentric.de>
This commit is contained in:
Jonathan Burke
2020-04-09 13:17:20 -07:00
committed by GitHub
co-authored by Jonathan Burke
parent 0448b7d88e
commit 1478539f98
2 changed files with 27 additions and 6 deletions
+9 -6
View File
@@ -98,12 +98,15 @@ declare module "mongoose" {
* which would result in a passing conditional, because `never` is included in all types.
*/
export type MongooseFilterQuery<T> = {
[P in keyof T]?: P extends '_id' ?
mongodb.Condition<string | mongodb.ObjectId | { _id: mongodb.ObjectId }> :
[Extract<T[P], mongodb.ObjectId>] extends [never] ?
mongodb.Condition<T[P]> :
mongodb.Condition<T[P] | string>;
} & mongodb.RootQuerySelector<T>;
[P in keyof T]?: P extends '_id'
? [Extract<T[P], mongodb.ObjectId>] extends [never]
? mongodb.Condition<T[P]>
: mongodb.Condition<T[P] | string | { _id: mongodb.ObjectId }>
: [Extract<T[P], mongodb.ObjectId>] extends [never]
? mongodb.Condition<T[P]>
: mongodb.Condition<T[P] | string>;
} &
mongodb.RootQuerySelector<T>;
/* FilterQuery alias type for using as type for filter/conditions parameters */
export type FilterQuery<T> = MongooseFilterQuery<DocumentDefinition<T>>;
+18
View File
@@ -2447,3 +2447,21 @@ Animal2.find().distinct('_id').byName('fido').exec(function(err, animal) {
Animal2.findOne().where({ type: 'dog' }).byName('fido').exec(function(err, animal) {
console.log(animal);
});
/* Filter query */
interface Foobar extends mongoose.Document {
_id: number;
name: string;
type: string;
tags: string[];
}
var foobarSchema = new mongoose.Schema({
_id: Number,
name: String,
type: String,
tags: { type: [String], index: true } // field level
});
var Foobar = mongoose.model<Foobar, mongoose.Model<Foobar>>('AnimFoobarl', foobarSchema);
Foobar.find({ _id: 123 });