Microsoft/TypeScript

Mixin with abstract class infers wrong type for super

Open

#60.315 aberto em 22 de out. de 2024

Ver no GitHub
 (1 comment) (2 reactions) (0 assignees)TypeScript (6.726 forks)batch import
BugDomain: classesHelp Wanted

Métricas do repositório

Stars
 (48.455 stars)
Métricas de merge de PR
 (Mesclagem média 6d 17h) (9 fundiu PRs em 30d)

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

https://www.typescriptlang.org/play/?ts=5.7.0-dev.20241022#code/IYIwzgLgTsDGEAJYBthjAgsgTwILmjggGFV0EBvAKAQQAcBXEZAS1gVEhngQHMBTCADlgAW34AKAJQAuBFxYA7XgG4qAXypUAZg0XwWAe0UJRLAB5KA6iwgALfFyKk0YADwAVAEJp+CfuYQ-IoAJhiK-ADuCBIAdPHAULxgcsCK2ADaALpSCAC8AHxYeATcJGRgBRIA+iC+ct6+udQ0SBVYFkou5AFBoRi1vpRatLSMzGx8giLi0nIKysOjo1CCDFAmYAx0-FCxAsJiklIqCAD0ZwgAolBQhlByAESOhDzi9oYhCADkBzP83wQSjarh+OBeZW6YEBsDSikMiBAfjgsH46H4XwAbixgPJtrt-OY6Kt0EZFLFHq1aJpqSMEKsIOsTJhOoooWpNFoqEoglBtHA-DgAJKKXn81FLKaHWayeTQJSqDRaXT6CBk0ysmz2EVigWeHxgPy9YJhBARaJxBJJFIcdLZXKFYo63bi-hVQaGhoG-jNOkoUEsyxs9rG-oID1+FrLcasdh-I5zOVQBWS5YMpl4nZ7eMy04XBCRe4AazAVIQNPLdPTGw6QfZGiAA

💻 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

Guia do colaborador