[mongoose] Fix _id type of lean() result (#43039)

This commit is contained in:
Valentin Agachi 2020-03-10 19:04:18 +00:00 committed by GitHub
parent 0ea91f4387
commit 460862aa68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View File

@ -79,7 +79,7 @@ declare module "mongoose" {
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
/* Helper type to extract a definition type from a Document type */
type DocumentDefinition<T> = Omit<T, keyof Document> & { _id: mongodb.ObjectId };
type DocumentDefinition<T> = Omit<T, Exclude<keyof Document, '_id'>>;
/**
* Gets and optionally overwrites the function used to pluralize collection names

View File

@ -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 = <mongoose.DocumentQuery<Location1, Location1>>{};
async function leanTests() {
var location = await locQuery.lean().exec();
var loc1Query = <mongoose.DocumentQuery<Location1, Location1>>{};
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 = <mongoose.DocumentQuery<Location2, Location2>>{};
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;