microsoft / microsoft/TypeScript
Type narrowing of `super` when using `this`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Bug Report
🔎 Search Terms
super, this, class
🕗 Version & Regression Information
Nightly
⏯ Playground Link
💻 Code
class Animal<T> {
eat<T>(this: Animal<T>) {
throw new Error('This doesnt matter');
}
}
class Dog<T> extends Animal<T> {
eat<U>(this: Dog<U>) {
super.eat(); // The type is Animal<T>.eat<T>(this: Animal<T>): void
// The type should be Animal<U>.eat<U>(this: Animal<U>): void
}
}
🙁 Actual behavior
super.eat() is typed as Animal<T>.eat<T>(this: Animal<T>): void
🙂 Expected behavior
super.eat() should be typed as Animal<U>.eat<U>(this: Animal<U>): void since this is declared as Dog<U> which is a subclass of Animal<U>.
One can workaround this by writing super.eat<U>(), but the signature then becomes goofy (it becomes Animal<T>.eat<U>(this: Animal<U>): void)
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 il TypeScript Playground collegato e riproduci la differenza tra i tipi correnti e quelli previsti per super.eat() nelle classi generiche Animal e Dog. Traccia la gestione dei tipi this da parte del compilatore nell'accesso alle proprietà di super, quindi aggiungi un test di regressione e conferma che super.eat() preservi il tipo specifico di U senza richiedere un argomento di tipo esplicito.
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
- 38/100