Microsoft/TypeScript
GitHub ã§èŠãIncorrectly reported `TS2322` error on array items after spread
Open
#55,340 opened on 2023幎8æ11æ¥
BugDomain: Error MessagesHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð 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.