microsoft / microsoft/TypeScript
Narrowing of generic this is inconsistent with variable narrowing
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
🔎 Search Terms
generic union constraint narrowing this
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
type Type = I32 | Str;
interface Str {
group: "none";
kind: "str";
}
interface I32 {
group: "scalar";
kind: "i32";
}
declare function takeI32(value: I32): void;
declare function inferKind<T>(value: { kind: T }): T;
function testThis<T extends Type>(this: T) {
switch (this.group) {
case "none":
return;
case "scalar":
switch (this.kind) {
case "str": // no error?
case "i32":
}
}
}
function testParameter<T extends Type>(o: T) {
switch (o.group) {
case "none":
return;
case "scalar":
switch (o.kind) {
case "str": // error!
case "i32":
}
}
}
function testAliasedThis<T extends Type>(this: T) {
const o = this;
switch (o.group) {
case "none":
return;
case "scalar":
switch (o.kind) {
case "str": // error!
case "i32":
}
}
}
function testThis2<T extends Type>(this: T): I32 | undefined {
if (this.group === "scalar") {
const value: I32 = this; // error
takeI32(this); // error
this satisfies I32; // error
const arrow = (): I32 => this; // error
return this; // error
}
}
function testParameter2<T extends Type>(o: T): I32 | undefined {
if (o.group === "scalar") {
const value: I32 = o; // ok
takeI32(o); // ok
o satisfies I32; // ok
const arrow = (): I32 => o; // ok
return o; // ok
}
}
function testAliasedThis2<T extends Type>(this: T): I32 | undefined {
const o = this;
if (o.group === "scalar") {
const value: I32 = o; // ok
takeI32(o); // ok
o satisfies I32; // ok
const arrow = (): I32 => o; // ok
return o; // ok
}
}
🙁 Actual behavior
The errors (or lack of them) are inconsistent between narrowing this directly vs narrowing an "aliased" this (const alias = this;)
🙂 Expected behavior
They should be consistent
Additional information about the issue
No response
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 und reduzieren Sie die bereitgestellten generic-union-Beispiele für direct this, aliased this und parameter narrowing. Da im Issue keine Repository-Datei oder kein Test angegeben ist, lokalisieren Sie den Einstiegspunkt für das Narrowing im Compiler und fügen Sie einen Regressionstest hinzu, der die gezeigten Fälle abdeckt. Als erledigt gilt die Aufgabe, wenn das Narrowing von direktem und aliasiertem this konsistente Diagnosen erzeugt.
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
- Aktiv
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100