diff --git a/test/types/lean.test.ts b/test/types/lean.test.ts index 745999d3aa..f9b4a66be0 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 668f67e55d..2ce71b4e8a 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; /**