microsoft / microsoft/TypeScript
JS ES6 class derived member functions typed with literals don't get assigned the correct type
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
In rare cases (such as when literals are used in a type constraint), ES6 class overridden functions will cause strange issues when returning said literals from the derived function.
🔎 Search Terms
ts es6 class derived
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about ES6 classes
⏯ Playground Link
Playground link with relevant code
💻 Code
class Base
{
test()
{
if (Math.random() > 0.5)
return 5;
return false;
}
}
class Derived extends Base
{
// error here: for some reason, Derived::test() is typed as "() => boolean"
// even though the literal false is being returned, and as such it says it can't
// match Base::test
test()
{
return false;
}
}
🙁 Actual behavior
The derived function's auto-typing assumes it is the literals' actual type, such as number or boolean, instead of the literals themselves. Derived::test, if not overridden, is typed as () => false, which should match () => 5 | false, but because it's typed as boolean instead it breaks (this only seems to happen if we're overriding a base member function)
🙂 Expected behavior
The derived function should be able to match if the typing is handled as normal.
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 tipi inferiti per Base::test e Derived::test. Traccia il modo in cui i metodi ES6 sovrascritti inferiscono tipi di ritorno letterali, quindi verifica che la funzione derivata venga accettata rispetto all'unione letterale della base senza perdere i tipi letterali.
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