From 2667312c664da2779f65477b606398ca2c7a376c Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Tue, 4 Jul 2023 08:34:54 +0300 Subject: [PATCH] Avoid using Omit in HydratedDocument when not needed See https://github.com/microsoft/TypeScript/issues/39556 and many other related issues. Fixes #13529. --- test/types/models.test.ts | 18 ++++++++++++++++++ types/index.d.ts | 2 +- types/utility.d.ts | 9 ++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/test/types/models.test.ts b/test/types/models.test.ts index 3f95d61e271..99f311540bc 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -622,3 +622,21 @@ function gh13206() { expectType>(change); }); } + +function gh13529() { + interface ResourceDoc { + foo: string; + } + + type ResourceIdT = { + [key in ResourceIdField]: string; + }; + type ResourceDocWithId = ResourceDoc & ResourceIdT; + + function test< + ResourceType extends string, + DocT extends ResourceDocWithId<`${ResourceType}Id`>>(dbModel: Model) { + const resourceDoc = new dbModel(); + resourceDoc.foo = 'bar'; + } +} 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".