Mixin with abstract class infers wrong type for super
#60,315 opened on 2024幎10æ22æ¥
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð 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