microsoft / microsoft/TypeScript
Mixin with abstract class infers wrong type for super
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
"mixin abstract super", "mixin abstract super type", "mixin abstract constructor super", "mixin abstract constructor super type", "mixin Abstract method cannot be accessed via super expression"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "super"
⏯ Playground Link
💻 Code
abstract class MyAbstractClass {
public abstract getName(): string;
}
function mixinWithAbstractClass<TBase extends new (...args: any[]) => MyAbstractClass>(_base: TBase) {
class MixinClass extends _base {
public getName(): string {
return super.getName(); // Error: "Abstract method 'getName' in class 'MyAbstractClass' cannot be accessed via super expression."
}
}
return MixinClass;
}
interface MyInterface {
getName(): string;
}
function mixinWithInterface<TBase extends new (...args: any[]) => MyInterface>(_base: TBase) {
class MixinClass extends _base {
public getName(): string {
return super.getName(); // works as expected
}
}
return MixinClass;
}
🙁 Actual behavior
mixinWithAbstractClass fails because the compiler infers that super is of the exact type class MyAbstractClass.
🙂 Expected behavior
I expected mixinWithAbstractClass to compile like mixinWithInterface does. The constraint TBase extends new (...args: any[]) => MyAbstractClass specifies that TBase extends a concrete constructor, which returns instances that conform to MyAbstractClass. Since the constructor is concrete, we can infer that TBase (the superclass) already implements the abstract methods.
Additional information about the issue
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare the mixinWithAbstractClass and mixinWithInterface cases, focusing on how the compiler determines the type of super. Trace the type-checking path for abstract methods in a generic class extending _base; done means the abstract-class mixin accepts super.getName() without regressing the interface case, with a regression test for the reproduction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100