Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(query): make lean() flatten out inferred maps into Record<string, V> #13326

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
],
"license": "MIT",
"dependencies": {
"bson": "^5.0.1",
"bson": "^5.2.0",
"kareem": "2.5.1",
"mongodb": "5.1.0",
"mongodb": "5.3.0",
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
"mpath": "0.9.0",
"mquery": "5.0.0",
"ms": "2.1.3",
Expand Down
18 changes: 18 additions & 0 deletions test/types/lean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,21 @@ async function _11767() {
// expectError(examFound2Obj.questions[0].populated);
expectType<string[]>(examFound2Obj.questions[0].answers);
}

async function gh13010() {
const schema = new Schema({
name: { required: true, type: Map, of: String }
});

const CountryModel = model('Country', schema);

await CountryModel.create({
name: {
en: 'Croatia',
ru: 'Хорватия'
}
});

const country = await CountryModel.findOne().lean().orFail().exec();
expectType<Record<string, string>>(country.name);
}
6 changes: 3 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,13 @@ declare module 'mongoose' {
export type UpdateQuery<T> = _UpdateQuery<T> & AnyObject;

export type FlattenMaps<T> = {
[K in keyof T]: T[K] extends Map<any, any>
? AnyObject : T[K] extends TreatAsPrimitives
[K in keyof T]: T[K] extends Map<any, infer V>
? Record<string, V> : T[K] extends TreatAsPrimitives
? T[K] : FlattenMaps<T[K]>;
};

export type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;
export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId;
export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId | Buffer | Function;

export type SchemaDefinitionType<T> = T extends Document ? Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>> : T;

Expand Down
2 changes: 1 addition & 1 deletion types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ declare module 'mongoose' {
j(val: boolean | null): this;

/** Sets the lean option. */
lean<LeanResultType = ResultType extends any[] ? Require_id<RawDocType>[] : Require_id<RawDocType>>(val?: boolean | any): QueryWithHelpers<ResultType extends null ? LeanResultType | null : LeanResultType, DocType, THelpers, RawDocType>;
lean<LeanResultType = ResultType extends any[] ? Require_id<FlattenMaps<RawDocType>>[] : Require_id<FlattenMaps<RawDocType>>>(val?: boolean | any): QueryWithHelpers<ResultType extends null ? LeanResultType | null : LeanResultType, DocType, THelpers, RawDocType>;

/** Specifies the maximum number of documents the query will return. */
limit(val: number): this;
Expand Down