microsoft / microsoft/TypeScript
Discriminant-specific error message not shown when one constituent of a union has a union-typed discriminant
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 3.9.0-dev.20200319
Search Terms: discrimant tagged union assignable
Code
A simplified repro, based on real-world code:
interface A { x: number }
interface B { x: string }
interface U$A { kind: 'A', body: A }
interface U$B { kind: 'B', body: B }
interface U$null { kind: 'C' | 'D', body?: undefined }
type U = U$A | U$B | U$null;
declare function foo(u: U): void;
foo({kind: 'A', body: { x: 42 }}); // OK
foo({kind: 'B', body: { x: 42 }}); // Not OK
Expected behavior:
The second x: 42 gets a squiggle and and the error Type 'number' is not assignable to type 'string'.
Actual behavior:
The whole argument to the second foo call gets a squiggle and the error:
Argument of type '{ kind: "B"; body: { x: number; }; }' is not assignable to parameter of type 'U'. Type '{ kind: "B"; body: { x: number; }; }' is not assignable to type 'U$null'. Types of property 'kind' are incompatible. Type '"B"' is not assignable to type '"C" | "D"'.
I would expect the type of the argument to have been narrowed to U$B by checking the kind property first. As it stands, the developer UX is not ideal especially when the shapes of the types involved get larger and it gets quite hard to figure out which property is wrong (in my real-world case, the union has 250+ members and the error message is incomprehensible).
Notes:
If I change x to y in interface B, the error message comes up OK.
Also, if I comment out the two references to U$null, I get a different (but also acceptable) error.
Related Issues:
None that (at an uneducated guess) seemed to be exactly this one.
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 fornita in TypeScript Playground e confronta la diagnostica prodotta per la seconda chiamata a foo con l’errore atteso specifico del discriminante. Segui il percorso del type-checker che gestisce le unioni con discriminanti di tipo unione; il lavoro è completato quando l’errore punta a x: 42 e segnala che number non è assegnabile a string, senza causare regressioni nelle varianti indicate.
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
- 32/100