microsoft / microsoft/TypeScript
Generic methods with type parameters in generic constraints cannot be overridden
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
TypeScript Version: 3.0.0-dev.20180630
Search Terms: generic method, subclass, override, type parameter constraint
Code
class A {
m<T, U extends T>(t: T, u: U) { }
}
class B extends A {
m<T, U extends T>(t: T, u: U) { }
// ﹋ error
}
Expected behavior:
No errors; the m method in the subclass has exactly the same signature as the method it is overriding.
Actual behavior:
The compiler warns on the method override B.m:
Type '<T, U extends T>(t: T, u: U) => void' is not assignable
to type '<T, U extends T>(t: T, u: U) => void'.
Two different types with this name exist, but they are unrelated.
Types of parameters 'u' and 'u' are incompatible.
Type 'U' is not assignable to type 'T'.
Type 'T' is not assignable to type 'T'.
Two different types with this name exist, but they are unrelated.
Note that even if you change the names of the type parameters in method B.m, as in:
class B extends A {
m<T2, U2 extends T2>(t: T2, u: U2) { }
}
you still get the error Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
If you remove the extends T generic constraint, it compiles.
Playground Link:
here
Related Issues:
Nothing obviously related; #23960 maybe?
So, is this a compiler bug? Or is it intended, and if so, what's the reasoning for it? This issue is inspired by a Stack Overflow question that left me scratching my head.
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
Beginne damit, das gemeldete Beispiel im TypeScript playground oder in einem Compiler-Test zu reproduzieren, wobei generische Methoden mit einem eingeschränkten Typparameter in einer Unterklasse verwendet werden. Verfolge die Override-Kompatibilitätsprüfung für die beiden Methodensignaturen und füge einen Regressionstest hinzu, der zeigt, dass das äquivalente Override akzeptiert wird, während die Einschränkung weiterhin durchgesetzt 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