microsoft / microsoft/TypeScript
Type narrowing discards template type on this-typed return value
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Bug Report
🔎 Search Terms
type narrowing template constraint
🕗 Version & Regression Information
Bug occurs in every version on the playground
⏯ Playground Link
Playground link with relevant code
💻 Code
abstract class Foo {
abstract clone(): this;
}
function cloneMe<T extends Foo|Date>(x: T): T {
if (x instanceof Foo) return x.clone();
// These casts are necessary because of https://github.com/microsoft/TypeScript/issues/38309.
return new Date(x as Date) as T;
}
🙁 Actual behavior
Type 'Foo & Foo' is not assignable to type 'T'. 'Foo & Foo' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Foo | Date'.
🙂 Expected behavior
No errors; just like the instanceof check narrows x to Foo & T, the this return type of x.clone() should also be Foo & T rather than degrading to Foo & Foo.
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 l’esempio collegato di TypeScript Playground ed esamina il restringimento dei tipi e il comportamento di ritorno this polimorfico coinvolti in cloneMe. L’issue è completata quando il valore restituito da x.clone() viene preservato come Foo & T e l’esempio verifica i tipi senza cast né errori.
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