microsoft / microsoft/TypeScript
Incorrectly reported `TS2322` error on array items after spread
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
"array spread", "TS2322"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
⏯ Playground Link
💻 Code
const a: Array<number | string> = [];
const b: Array<number> = [...a, 1];
🙁 Actual behavior
The compiler gives the following two errors, of which only one is appropriate:
~/prog/typescript ❯ tsc ./array-spread-error.ts
array-spread-error.ts:2:27 - error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
2 const b: Array<number> = [...a, 1];
~~~~
array-spread-error.ts:2:33 - error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
2 const b: Array<number> = [...a, 1];
~
🙂 Expected behavior
The spread is clearly incorrect, as the original array could potentially contain strings as well, but the number literal should not be marked with the same error.
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 conferma che lo spread e il literal numerico che lo segue producono diagnosi TS2322 separate. Traccia il percorso del checker per gli array literal con elementi spread e aggiungi un test di regressione che mostri che viene segnalato solo lo spread non valido, mentre il literal numerico viene accettato.
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
- 42/100