Skip to content
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

fix: Generic static This. #2533

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion typings/objection/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,19 @@ declare namespace Objection {
...params: TParams
) => TResult;

interface PrototypeType<T> extends Function {
prototype: T;
}

interface ConstructorFunctionType<T = any> extends PrototypeType<T> {
new (...args: any[]): T;
}

// for internal use on generic static this deduction, copied from https://github.com/microsoft/TypeScript/issues/5863#issuecomment-1483978415
type ConstructorType<T = unknown, Static extends Record<string, any> = PrototypeType<T>> = (ConstructorFunctionType<T> | PrototypeType<T>) & {
[Key in keyof Static]: Static[Key];
};

export interface ModelConstructor<M extends Model> extends Constructor<M> {}

export interface ModelClass<M extends Model> extends ModelConstructor<M> {
Expand Down Expand Up @@ -1569,7 +1582,7 @@ declare namespace Objection {
): QueryBuilderType<M>;

static relatedQuery<M extends Model, K extends keyof M>(
this: Constructor<M>,
this: ConstructorType<M>,
relationName: K,
trxOrKnex?: TransactionOrKnex,
): ArrayRelatedQueryBuilder<M[K]>;
Expand Down