diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index ec20ce689e..467e619236 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -20,8 +20,10 @@ import { Types, Query, model, - ValidateOpts + ValidateOpts, + BufferToBinary } from 'mongoose'; +import { Binary } from 'mongodb'; import { IsPathRequired } from '../../types/inferschematype'; import { expectType, expectError, expectAssignable } from 'tsd'; import { ObtainDocumentPathType, ResolvePathType } from '../../types/inferschematype'; @@ -917,7 +919,7 @@ async function gh12593() { expectType(doc2.x); const doc3 = await Test.findOne({}).orFail().lean(); - expectType(doc3.x); + expectType(doc3.x); const arrSchema = new Schema({ arr: [{ type: Schema.Types.UUID }] }); @@ -1663,3 +1665,19 @@ async function gh14950() { expectType(doc.location!.type); expectType(doc.location!.coordinates); } + +async function gh14902() { + const exampleSchema = new Schema({ + image: { type: Buffer }, + subdoc: { + type: new Schema({ + testBuf: Buffer + }) + } + }); + const Test = model('Test', exampleSchema); + + const doc = await Test.findOne().lean().orFail(); + expectType(doc.image); + expectType(doc.subdoc!.testBuf); +} diff --git a/types/index.d.ts b/types/index.d.ts index 3ec72ac281..c6655b802f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -706,6 +706,18 @@ declare module 'mongoose' { [K in keyof T]: FlattenProperty; }; + 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 + ? Types.DocumentArray> + : T[K] extends Types.Subdocument + ? HydratedSingleSubdocument + : BufferToBinary; + } : T; + /** * Separate type is needed for properties of union type (for example, Types.DocumentArray | undefined) to apply conditional check to each member of it * https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types @@ -716,7 +728,7 @@ declare module 'mongoose' { ? Types.DocumentArray> : FlattenMaps; export type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined; - export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId | Buffer | Function; + export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId | Buffer | Function | mongodb.Binary; export type SchemaDefinitionType = T extends Document ? Omit> : T; diff --git a/types/query.d.ts b/types/query.d.ts index cb447f23c7..9d2114edff 100644 --- a/types/query.d.ts +++ b/types/query.d.ts @@ -211,7 +211,7 @@ declare module 'mongoose' { type QueryOpThatReturnsDocument = 'find' | 'findOne' | 'findOneAndUpdate' | 'findOneAndReplace' | 'findOneAndDelete'; type GetLeanResultType = QueryOp extends QueryOpThatReturnsDocument - ? (ResultType extends any[] ? Require_id>[] : Require_id>) + ? (ResultType extends any[] ? Require_id>>[] : Require_id>>) : ResultType; type MergePopulatePaths> = QueryOp extends QueryOpThatReturnsDocument