Skip to content

Commit

Permalink
Merge pull request #15261 from Automattic/vkarpov15/gh-15121-3
Browse files Browse the repository at this point in the history
types: make type inference logic resilient to no Buffer type due to missing `@types/node`
  • Loading branch information
vkarpov15 authored Feb 18, 2025
2 parents d64f481 + 864ac87 commit 987f1ad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
74 changes: 40 additions & 34 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,47 +712,53 @@ declare module 'mongoose' {
[K in keyof T]: FlattenProperty<T[K]>;
};

export type BufferToBinaryProperty<T> = T extends Buffer
? mongodb.Binary
: T extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<BufferToBinary<ItemType>>
: T extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
: BufferToBinary<T>;
export type BufferToBinaryProperty<T> = unknown extends Buffer
? T
: T extends Buffer
? mongodb.Binary
: T extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<BufferToBinary<ItemType>>
: T extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<BufferToBinary<SubdocType>>
: BufferToBinary<T>;

/**
* Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns
*/
export type BufferToBinary<T> = T extends Buffer
? mongodb.Binary
: T extends Document
? T
: T extends TreatAsPrimitives
? T
: T extends Record<string, any>
? {
[K in keyof T]: BufferToBinaryProperty<T[K]>
}
: T;
export type BufferToBinary<T> = unknown extends Buffer
? T
: T extends Buffer
? mongodb.Binary
: T extends Document
? T
: T extends TreatAsPrimitives
? T
: T extends Record<string, any>
? {
[K in keyof T]: BufferToBinaryProperty<T[K]>
}
: T;

/**
* Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
*/
export type BufferToJSON<T> = T extends Buffer
? { type: 'buffer', data: number[] }
: T extends Document
? T
: T extends TreatAsPrimitives
* Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
*/
export type BufferToJSON<T> = unknown extends Buffer
? T
: T extends Buffer
? { type: 'buffer', data: number[] }
: T extends Document
? T
: T extends Record<string, any> ? {
[K in keyof T]: T[K] extends Buffer
? { type: 'buffer', data: number[] }
: T[K] extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<BufferToBinary<ItemType>>
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<SubdocType>
: BufferToBinary<T[K]>;
} : T;
: T extends TreatAsPrimitives
? T
: T extends Record<string, any> ? {
[K in keyof T]: T[K] extends Buffer
? { type: 'buffer', data: number[] }
: T[K] extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<BufferToBinary<ItemType>>
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<SubdocType>
: BufferToBinary<T[K]>;
} : T;

/**
* Converts any ObjectId properties into strings for JSON serialization
Expand Down
12 changes: 8 additions & 4 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,17 @@ type IsSchemaTypeFromBuiltinClass<T> = T extends (typeof String)
? true
: T extends Types.Decimal128
? true
: T extends Buffer
: T extends NativeDate
? true
: T extends NativeDate
: T extends (typeof Schema.Types.Mixed)
? true
: T extends (typeof Schema.Types.Mixed)
: IfEquals<T, Schema.Types.ObjectId, true, false> extends true
? true
: IfEquals<T, Schema.Types.ObjectId, true, false>;
: unknown extends Buffer
? false
: T extends Buffer
? true
: false;

/**
* @summary Resolve path type by returning the corresponding type.
Expand Down

0 comments on commit 987f1ad

Please sign in to comment.