microsoft / microsoft/TypeScript
Generic type comparison results in incorrect error when inferring from undefined type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
"generics", "infer undefined type", "not assignable"
🕗 Version & Regression Information
- This changed between versions 3.6.3 and 3.7.5
⏯ Playground Link
💻 Code
type Generic<T> = {
// In this scenario, "InvalidType" is not defined
x: T extends InvalidType<infer S> ? S : 0
}
// Correct behaviour:
// Exactly matching generic type works correctly
const correct1: Generic<number> = {} as Generic<number>
// Non-matching generic type results in error (expected)
const correct2: Generic<number> = {} as Generic<string>
// Unexpected behavior
// More specific generic type results in a weird error:
// Type 'Generic<42>' is not assignable to type 'Generic<number>'.
// Type 'number' is not assignable to type '42'.
const invalid: Generic<number> = {} as Generic<42>
🙁 Actual behavior
TS error:
Type 'Generic<42>' is not assignable to type 'Generic<number>'.
Type 'number' is not assignable to type '42'.
🙂 Expected behavior
There should be no TypeScript error, or at least not a confusing error message like that.
It is not clear where the actual error comes from (undefined type InvalidType), and the current
error message does not really make sense - to a user it looks like the type comparison (42 -> number) was inverted (number -> 42).
Additional information about the issue
This also seems to happen in more complex scenarios where all involved types are correctly defined but another TS error like Type instantiation is excessively deep and possibly infinite occurs somewhere in the chain.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione collegata in TypeScript Playground e confronta il comportamento segnalato tra le versioni indicate, 3.6.3 e 3.7.5. Traccia il percorso del controllo dei tipi per Generic quando InvalidType non è definito, incluso lo scenario aggiuntivo di istanziazione profonda; il lavoro è completato quando l’esempio non produce errori oppure produce una diagnostica chiara e correttamente orientata.
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
- Abbastanza chiara
- Idoneità per principianti
- 42/100