Skip to content

Commit

Permalink
Avoid using Omit in HydratedDocument when not needed
Browse files Browse the repository at this point in the history
See microsoft/TypeScript#39556 and many other
related issues.

Fixes #13529.
  • Loading branch information
orgads committed Jul 4, 2023
1 parent 67b4224 commit 37e8d49
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
18 changes: 18 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,3 +1123,21 @@ function gh13534() {
const doc = new Test({ myId: '0'.repeat(24) });
expectType<Types.ObjectId>(doc.myId);
}

interface ResourceDoc {
foo: string;
}

type ResourceIdT<ResourceIdField extends string> = {
[key in ResourceIdField]: string;
};
type ResourceDocWithId<ResourceIdField extends string> = ResourceDoc & ResourceIdT<ResourceIdField>;

class TestClass<
ResourceType extends string,
DocT extends ResourceDocWithId<`${ResourceType}Id`>> {
constructor(dbModel: Model<DocT>) {
const resourceDoc = new dbModel();
expectType<string>(resourceDoc.foo);
}
}
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ declare module 'mongoose' {
> = IfAny<
DocType,
any,
Document<unknown, TQueryHelpers, DocType> & MergeType<
Document<unknown, TQueryHelpers, DocType> & MergeUnlessEmpty<
Require_id<DocType>,
TOverrides extends Record<string, never> ?
{} :
Expand Down
9 changes: 4 additions & 5 deletions types/utility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ declare module 'mongoose' {
U :
T extends ReadonlyArray<infer U> ? U : T;

type MergeType<A, B> = Omit<A, keyof B> & B;
type MergeUnlessEmpty<A, B> = keyof B extends never ? A : Omit<A, keyof B> & B;

type UnpackedIntersection<T, U> = T extends null ? null : T extends (infer A)[]
? (Omit<A, keyof U> & U)[]
: keyof U extends never
? T
: Omit<T, keyof U> & U;

type MergeType<A, B> = Omit<A, keyof B> & B;
: MergeUnlessEmpty<T, U>;

/**
* @summary Converts Unions to one record "object".
Expand Down

0 comments on commit 37e8d49

Please sign in to comment.