Skip to content

Avoid using Omit in HydratedDocument when not needed #13573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,21 @@ function gh13206() {
expectType<ChangeStreamInsertDocument<ITest>>(change);
});
}

function gh13529() {
interface ResourceDoc {
foo: string;
}

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

function test<
ResourceType extends string,
DocT extends ResourceDocWithId<`${ResourceType}Id`>>(dbModel: Model<DocT>) {
const resourceDoc = new dbModel();
resourceDoc.foo = 'bar';
}
}
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