diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 71b03c3eda..7b1a6abac8 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -79,7 +79,7 @@ declare module "mongoose" { type Omit = Pick>; /* Helper type to extract a definition type from a Document type */ - type DocumentDefinition = Omit & { _id: mongodb.ObjectId }; + type DocumentDefinition = Omit>; /** * Gets and optionally overwrites the function used to pluralize collection names diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index d864af7e89..38b1387a06 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -1303,14 +1303,14 @@ query.lean(false) query.lean({}) interface Location1 extends mongoose.Document { + _id: mongoose.Types.ObjectId; name: string; address: string; rating: number; reviews: any[]; }; -var locQuery = >{}; -async function leanTests() { - var location = await locQuery.lean().exec(); +var loc1Query = >{}; +loc1Query.lean().then(location => { if (location) { // $ExpectType ObjectId location._id; @@ -1323,7 +1323,28 @@ async function leanTests() { // $ExpectError location.save(); } -} +}); + +interface Location2 extends mongoose.Document { + _id: string; + name: string; + rating: number; +}; +var loc2Query = >{}; +loc2Query.lean().then(location => { + if (location) { + // $ExpectType string + location._id; + // $ExpectType string + location.name; + // $ExpectType number + location.rating; + // $ExpectError + location.unknown; + // $ExpectError + location.save(); + } +}); /* * section schema/array.js @@ -1965,6 +1986,7 @@ MongoModel.find({ .exec(); /* practical example */ interface Location extends mongoose.Document { + _id: mongoose.Types.ObjectId; name: string; address: string; rating: number;