From d3c957e80fa30b744e55b3d963dffd4e452fea03 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 3 Dec 2024 12:01:14 -0500 Subject: [PATCH] types: only apply BufferToBinary if type strictly equals Buffer | null | undefined Fix #15057 --- test/types/lean.test.ts | 31 +++++++++++++++++++++++++++++ types/index.d.ts | 44 ++++++++++++++++++++++++++--------------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/test/types/lean.test.ts b/test/types/lean.test.ts index 745999d3aa0..f9b4a66be02 100644 --- a/test/types/lean.test.ts +++ b/test/types/lean.test.ts @@ -206,3 +206,34 @@ async function gh13382() { const res = await Test.updateOne({}, { name: 'bar' }).lean(); expectAssignable<{ matchedCount: number, modifiedCount: number }>(res); } + +async function gh15057() { + type Attachment = + | { + type: 'foo'; + value?: undefined; + } + | { + type: 'string'; + value?: string; + }; + + const TestSchema = new Schema({ + type: { type: String, required: true }, + value: { type: String } + }); + + const AttachmentModel = model('test', TestSchema); + + const main = async() => { + const item = await AttachmentModel.findOne().lean(); + + if (!item) return; + + doSomeThing(item); + }; + + const doSomeThing = (item: Attachment) => { + console.log(item); + }; +} diff --git a/types/index.d.ts b/types/index.d.ts index 668f67e55d1..2ce71b4e8aa 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -715,13 +715,16 @@ declare module 'mongoose' { export type BufferToBinary = T extends TreatAsPrimitives ? T : T extends Record ? { [K in keyof T]: T[K] extends Buffer ? mongodb.Binary - : T[K] extends (Buffer | null | undefined) - ? mongodb.Binary | null | undefined - : T[K] extends Types.DocumentArray + : IfEquals< + T[K], + Buffer | null | undefined, + mongodb.Binary | null | undefined, + T[K] extends Types.DocumentArray ? Types.DocumentArray> : T[K] extends Types.Subdocument ? HydratedSingleSubdocument - : BufferToBinary; + : BufferToBinary + >; } : T; /** @@ -730,13 +733,16 @@ declare module 'mongoose' { export type BufferToJSON = T extends TreatAsPrimitives ? T : T extends Record ? { [K in keyof T]: T[K] extends Buffer ? { type: 'buffer', data: number[] } - : T[K] extends (Buffer | null | undefined) - ? { type: 'buffer', data: number[] } | null | undefined - : T[K] extends Types.DocumentArray + : IfEquals< + T[K], + Buffer | null | undefined, + { type: 'buffer', data: number[] } | null | undefined, + T[K] extends Types.DocumentArray ? Types.DocumentArray> : T[K] extends Types.Subdocument ? HydratedSingleSubdocument - : BufferToBinary; + : BufferToBinary + > } : T; /** @@ -745,13 +751,16 @@ declare module 'mongoose' { export type ObjectIdToString = T extends TreatAsPrimitives ? T : T extends Record ? { [K in keyof T]: T[K] extends mongodb.ObjectId ? string - : T[K] extends (mongodb.ObjectId | null | undefined) - ? string | null | undefined - : T[K] extends Types.DocumentArray + : IfEquals< + T[K], + mongodb.ObjectId | null | undefined, + string | null | undefined, + T[K] extends Types.DocumentArray ? Types.DocumentArray> : T[K] extends Types.Subdocument ? HydratedSingleSubdocument> - : ObjectIdToString; + : ObjectIdToString + > } : T; /** @@ -760,13 +769,16 @@ declare module 'mongoose' { export type DateToString = T extends TreatAsPrimitives ? T : T extends Record ? { [K in keyof T]: T[K] extends NativeDate ? string - : T[K] extends (NativeDate | null | undefined) - ? string | null | undefined - : T[K] extends Types.DocumentArray + : IfEquals< + T[K], + NativeDate | null | undefined, + string | null | undefined, + T[K] extends Types.DocumentArray ? Types.DocumentArray> : T[K] extends Types.Subdocument ? HydratedSingleSubdocument> - : DateToString; + : DateToString + > } : T; /**