Skip to content

Commit 2667312

Browse files
committed
Avoid using Omit in HydratedDocument when not needed
See microsoft/TypeScript#39556 and many other related issues. Fixes Automattic#13529.
1 parent 67b4224 commit 2667312

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

test/types/models.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,21 @@ function gh13206() {
622622
expectType<ChangeStreamInsertDocument<ITest>>(change);
623623
});
624624
}
625+
626+
function gh13529() {
627+
interface ResourceDoc {
628+
foo: string;
629+
}
630+
631+
type ResourceIdT<ResourceIdField extends string> = {
632+
[key in ResourceIdField]: string;
633+
};
634+
type ResourceDocWithId<ResourceIdField extends string> = ResourceDoc & ResourceIdT<ResourceIdField>;
635+
636+
function test<
637+
ResourceType extends string,
638+
DocT extends ResourceDocWithId<`${ResourceType}Id`>>(dbModel: Model<DocT>) {
639+
const resourceDoc = new dbModel();
640+
resourceDoc.foo = 'bar';
641+
}
642+
}

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ declare module 'mongoose' {
141141
> = IfAny<
142142
DocType,
143143
any,
144-
Document<unknown, TQueryHelpers, DocType> & MergeType<
144+
Document<unknown, TQueryHelpers, DocType> & MergeUnlessEmpty<
145145
Require_id<DocType>,
146146
TOverrides extends Record<string, never> ?
147147
{} :

types/utility.d.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ declare module 'mongoose' {
66
U :
77
T extends ReadonlyArray<infer U> ? U : T;
88

9+
type MergeType<A, B> = Omit<A, keyof B> & B;
10+
type MergeUnlessEmpty<A, B> = keyof B extends never ? A : Omit<A, keyof B> & B;
11+
912
type UnpackedIntersection<T, U> = T extends null ? null : T extends (infer A)[]
1013
? (Omit<A, keyof U> & U)[]
11-
: keyof U extends never
12-
? T
13-
: Omit<T, keyof U> & U;
14-
15-
type MergeType<A, B> = Omit<A, keyof B> & B;
14+
: MergeUnlessEmpty<T, U>;
1615

1716
/**
1817
* @summary Converts Unions to one record "object".

0 commit comments

Comments
 (0)