Skip to content

Commit 987f1ad

Browse files
authored
Merge pull request #15261 from Automattic/vkarpov15/gh-15121-3
types: make type inference logic resilient to no Buffer type due to missing `@types/node`
2 parents d64f481 + 864ac87 commit 987f1ad

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

types/index.d.ts

+40-34
Original file line numberDiff line numberDiff line change
@@ -712,47 +712,53 @@ declare module 'mongoose' {
712712
[K in keyof T]: FlattenProperty<T[K]>;
713713
};
714714

715-
export type BufferToBinaryProperty<T> = T extends Buffer
716-
? mongodb.Binary
717-
: T extends Types.DocumentArray<infer ItemType>
718-
? Types.DocumentArray<BufferToBinary<ItemType>>
719-
: T extends Types.Subdocument<unknown, unknown, infer SubdocType>
720-
? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
721-
: BufferToBinary<T>;
715+
export type BufferToBinaryProperty<T> = unknown extends Buffer
716+
? T
717+
: T extends Buffer
718+
? mongodb.Binary
719+
: T extends Types.DocumentArray<infer ItemType>
720+
? Types.DocumentArray<BufferToBinary<ItemType>>
721+
: T extends Types.Subdocument<unknown, unknown, infer SubdocType>
722+
? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
723+
: BufferToBinary<T>;
722724

723725
/**
724726
* Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns
725727
*/
726-
export type BufferToBinary<T> = T extends Buffer
727-
? mongodb.Binary
728-
: T extends Document
729-
? T
730-
: T extends TreatAsPrimitives
731-
? T
732-
: T extends Record<string, any>
733-
? {
734-
[K in keyof T]: BufferToBinaryProperty<T[K]>
735-
}
736-
: T;
728+
export type BufferToBinary<T> = unknown extends Buffer
729+
? T
730+
: T extends Buffer
731+
? mongodb.Binary
732+
: T extends Document
733+
? T
734+
: T extends TreatAsPrimitives
735+
? T
736+
: T extends Record<string, any>
737+
? {
738+
[K in keyof T]: BufferToBinaryProperty<T[K]>
739+
}
740+
: T;
737741

738742
/**
739-
* Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
740-
*/
741-
export type BufferToJSON<T> = T extends Buffer
742-
? { type: 'buffer', data: number[] }
743-
: T extends Document
744-
? T
745-
: T extends TreatAsPrimitives
743+
* Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
744+
*/
745+
export type BufferToJSON<T> = unknown extends Buffer
746+
? T
747+
: T extends Buffer
748+
? { type: 'buffer', data: number[] }
749+
: T extends Document
746750
? T
747-
: T extends Record<string, any> ? {
748-
[K in keyof T]: T[K] extends Buffer
749-
? { type: 'buffer', data: number[] }
750-
: T[K] extends Types.DocumentArray<infer ItemType>
751-
? Types.DocumentArray<BufferToBinary<ItemType>>
752-
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
753-
? HydratedSingleSubdocument<SubdocType>
754-
: BufferToBinary<T[K]>;
755-
} : T;
751+
: T extends TreatAsPrimitives
752+
? T
753+
: T extends Record<string, any> ? {
754+
[K in keyof T]: T[K] extends Buffer
755+
? { type: 'buffer', data: number[] }
756+
: T[K] extends Types.DocumentArray<infer ItemType>
757+
? Types.DocumentArray<BufferToBinary<ItemType>>
758+
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
759+
? HydratedSingleSubdocument<SubdocType>
760+
: BufferToBinary<T[K]>;
761+
} : T;
756762

757763
/**
758764
* Converts any ObjectId properties into strings for JSON serialization

types/inferschematype.d.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,17 @@ type IsSchemaTypeFromBuiltinClass<T> = T extends (typeof String)
235235
? true
236236
: T extends Types.Decimal128
237237
? true
238-
: T extends Buffer
238+
: T extends NativeDate
239239
? true
240-
: T extends NativeDate
240+
: T extends (typeof Schema.Types.Mixed)
241241
? true
242-
: T extends (typeof Schema.Types.Mixed)
242+
: IfEquals<T, Schema.Types.ObjectId, true, false> extends true
243243
? true
244-
: IfEquals<T, Schema.Types.ObjectId, true, false>;
244+
: unknown extends Buffer
245+
? false
246+
: T extends Buffer
247+
? true
248+
: false;
245249

246250
/**
247251
* @summary Resolve path type by returning the corresponding type.

0 commit comments

Comments
 (0)