microsoft / microsoft/TypeScript
Mixin with abstract class infers wrong type for super
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione collegata di TypeScript Playground e confronta i casi mixinWithAbstractClass e mixinWithInterface, concentrandoti su come il compilatore determina il tipo di super. Traccia il percorso del controllo dei tipi per i metodi astratti in una classe generica che estende _base; il lavoro è completato quando il mixin di classe astratta accetta super.getName() senza introdurre una regressione nel caso dell’interfaccia, con un test di regressione per la riproduzione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100