microsoft / microsoft/TypeScript
Type narrowing discards template type on this-typed return value
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem verlinkten TypeScript Playground-Beispiel und untersuchen Sie die Typ-Eingrenzung sowie das Verhalten des polymorphen this-Rückgabetyps in cloneMe. Das Issue ist abgeschlossen, wenn der Rückgabewert von x.clone() als Foo & T erhalten bleibt und das Beispiel ohne Casts oder Fehler typgeprüft wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100