Skip to content

Commit

Permalink
types: only apply BufferToBinary if type strictly equals Buffer | nul…
Browse files Browse the repository at this point in the history
…l | undefined

Fix #15057
  • Loading branch information
vkarpov15 committed Dec 3, 2024
1 parent 76f92d2 commit d3c957e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
31 changes: 31 additions & 0 deletions test/types/lean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attachment>({
type: { type: String, required: true },
value: { type: String }
});

const AttachmentModel = model<Attachment>('test', TestSchema);

const main = async() => {
const item = await AttachmentModel.findOne().lean();

if (!item) return;

doSomeThing(item);
};

const doSomeThing = (item: Attachment) => {
console.log(item);
};
}
44 changes: 28 additions & 16 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,13 +715,16 @@ declare module 'mongoose' {
export type BufferToBinary<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
[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<infer ItemType>
: IfEquals<
T[K],
Buffer | null | undefined,
mongodb.Binary | null | undefined,
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]>;
: BufferToBinary<T[K]>
>;
} : T;

/**
Expand All @@ -730,13 +733,16 @@ declare module 'mongoose' {
export type BufferToJSON<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 (Buffer | null | undefined)
? { type: 'buffer', data: number[] } | null | undefined
: T[K] extends Types.DocumentArray<infer ItemType>
: IfEquals<
T[K],
Buffer | null | undefined,
{ type: 'buffer', data: number[] } | null | undefined,
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]>;
: BufferToBinary<T[K]>
>
} : T;

/**
Expand All @@ -745,13 +751,16 @@ declare module 'mongoose' {
export type ObjectIdToString<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
[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<infer ItemType>
: IfEquals<
T[K],
mongodb.ObjectId | null | undefined,
string | null | undefined,
T[K] extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<ObjectIdToString<ItemType>>
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<ObjectIdToString<SubdocType>>
: ObjectIdToString<T[K]>;
: ObjectIdToString<T[K]>
>
} : T;

/**
Expand All @@ -760,13 +769,16 @@ declare module 'mongoose' {
export type DateToString<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
[K in keyof T]: T[K] extends NativeDate
? string
: T[K] extends (NativeDate | null | undefined)
? string | null | undefined
: T[K] extends Types.DocumentArray<infer ItemType>
: IfEquals<
T[K],
NativeDate | null | undefined,
string | null | undefined,
T[K] extends Types.DocumentArray<infer ItemType>
? Types.DocumentArray<DateToString<ItemType>>
: T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
? HydratedSingleSubdocument<DateToString<SubdocType>>
: DateToString<T[K]>;
: DateToString<T[K]>
>
} : T;

/**
Expand Down

0 comments on commit d3c957e

Please sign in to comment.