Skip to content

feat: Allow mixin constructors with type arguments #51021

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

Conversation

bb010g
Copy link

@bb010g bb010g commented Oct 1, 2022

This constraint can be loosened without breakage.

Partially fixes #24122.

Example code that now works:

type Constructor<T> = new(...args: any[]) => T;

class A {
    a!: number;
}
class B {
    b!: number;
}
function mixinC<TBase extends Constructor<{}>>(Base: TBase) {
    return class C<T> extends Base {
        c!: T;
    };
}

const ACB = mixinC(A)<B>;
const acb = new ACB();
const acbC: B = acb.c;

Previously, compiling the class expression would fail with error TS2545:

tests/cases/compiler/mixinGeneric.ts:10:18 - error TS2545: A mixin class must have a constructor with a single rest parameter of type 'any[]'.

10     return class C<T> extends Base {
                    ~

It errors because unapplied C's constructor has a type signature of new <T>(...args: any[]) => C<T>. However, this doesn't turn out to be an issue. Code & types generate properly, and applying the resulting generic type properly monomorphizes new (e.g. ACB: new (...args: any[]) => mixinC<typeof A>.C<B>).

This constraint can be loosened without breakage.
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 1, 2022
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #24122. If you can get it accepted, this PR will have a better chance of being reviewed.

@sandersn
Copy link
Member

sandersn commented Dec 9, 2022

To help with PR housekeeping, I'm going to close this PR while it's still waiting on its bug to be accepted. Once the bug is accepted, we can move on to reviewing the code change. In the meantime it goes stale too easily to be useful.

@sandersn sandersn closed this Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Regression] TS2562 - mixins cannot accept generic types
3 participants