microsoft / microsoft/TypeScript

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

Aperta
#41,477 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il repro TypeScript e il link al Playground forniti, confrontando il comportamento del compilatore tra 3.6 e 3.7 come suggerito. Traccia il percorso del controllo dei tipi per i metodi generici di B che estende A; il lavoro è completato quando i casi a4 e a8 accettano true, mentre il comportamento esistente di a5, a6 e a7 rimane corretto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.