diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 7b926c26aca..0f64d8a57f0 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -1123,3 +1123,21 @@ function gh13534() { const doc = new Test({ myId: '0'.repeat(24) }); expectType(doc.myId); } + +interface ResourceDoc { + foo: string; +} + +type ResourceIdT = { + [key in ResourceIdField]: string; +}; +type ResourceDocWithId = ResourceDoc & ResourceIdT; + +class TestClass< + ResourceType extends string, + DocT extends ResourceDocWithId<`${ResourceType}Id`>> { + constructor(dbModel: Model) { + const resourceDoc = new dbModel(); + expectType(resourceDoc.foo); + } +} diff --git a/types/index.d.ts b/types/index.d.ts index 62d7a2ee5c2..794f184b80a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -141,7 +141,7 @@ declare module 'mongoose' { > = IfAny< DocType, any, - Document & MergeType< + Document & MergeUnlessEmpty< Require_id, TOverrides extends Record ? {} : diff --git a/types/utility.d.ts b/types/utility.d.ts index 69fe73a7be3..2f61efa17fb 100644 --- a/types/utility.d.ts +++ b/types/utility.d.ts @@ -6,13 +6,12 @@ declare module 'mongoose' { U : T extends ReadonlyArray ? U : T; + type MergeType = Omit & B; + type MergeUnlessEmpty = keyof B extends never ? A : Omit & B; + type UnpackedIntersection = T extends null ? null : T extends (infer A)[] ? (Omit & U)[] - : keyof U extends never - ? T - : Omit & U; - - type MergeType = Omit & B; + : MergeUnlessEmpty; /** * @summary Converts Unions to one record "object".