microsoft / microsoft/TypeScript

Generic methods with type parameters in generic constraints cannot be overridden

Aperta
#25,373 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: classes
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

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 riproducendo l’esempio segnalato nel TypeScript playground o in un test del compilatore, utilizzando metodi generici con un parametro di tipo vincolato in una sottoclasse. Traccia il controllo di compatibilità dell’override per le due firme dei metodi e aggiungi un test di regressione che dimostri che l’override equivalente viene accettato mentre il vincolo continua a essere applicato.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.