microsoft / microsoft/TypeScript

[BUG] union type generic extends child class with generic method

Offen
#41,477 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs Investigation
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

TypeScript Version: all, tested from 3.3 to 4.2

Search Terms: union type generic extends child class with generic method

Code

class A<GValue> {
  a<GKey extends GValue>(value: GKey): void {}
}

class B<GValue> extends A<GValue> {
  a<GKey extends GValue>(value: GKey): void {}
}


const a1: (('a' | 'b') extends any ? true : false) = true;
const a2: (('a') extends any ? true : false) = true;

const a3: (B<'a' | 'b'> extends A<any> ? true : false) = true; // error before v3.7
const a4: (B<'a'> extends A<any> ? true : false) = true; // error
const a5: (B<'a'> extends B<any> ? true : false) = true;

type Constrained<G extends A<any>> = true;

const a6: Constrained<A<'a'>> = true; // ok
const a7: Constrained<B<'a' | 'b'>> = true; // ok
const a8: Constrained<B<'a'>> = true; // error

Details:

  • a1 and a2 shows us that a string or an union of string extends any => legit

  • a3 shows us that it's still true, except before typescript 3.7 => true seems legit

  • a4 creates an error because (B<'a'> extends A<any> ? true : false) is actually false => bug, true expected.
    So, an union is true, but not a single value.

  • a5 is true which is correct

  • a6, a7 and a8 shows the same but with a type constraint

Notes:

  • a3 has not the same behavior between before and after v3.7
  • the bug appears only when we have a generic type on a method which extends another generic type coming from the class

Playground Link: playground link

Related Issues: this bug is pretty hard to reach and to describe, I was not able to find any related issue.
But a good start could be to search from a fix or update from the 3.6 to the 3.7, as a3 has not the same behavior between these versions.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit dem bereitgestellten TypeScript-Repro und dem Playground-Link und vergleichen Sie wie vorgeschlagen das Compilerverhalten zwischen 3.6 und 3.7. Verfolgen Sie den Type-Checking-Pfad für generische Methoden in B, das A erweitert; abgeschlossen ist die Aufgabe, wenn die Fälle a4 und a8 true akzeptieren, während das bestehende Verhalten von a5, a6 und a7 korrekt bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.