Description
TypeScript Version: 3.2.4, 3.3.0-dev.20190126
Search Terms: array, flat, concat, any, unsound
Code
const a: boolean[] = [[17], ["foo"]].flat(); // wrongly accepted
const b: boolean[] = Array.prototype.concat([17], [19], [21]); // wrongly accepted
Using tsc --strict -t esnext
.
(What I’m really trying to do: find a type safe way to concatenate an array of arrays. It’d be nice if [].concat(...arrays)
were accepted, which I think would be a consequence of #26976, but that’s not the issue I’m reporting here. arrays.flat()
and Array.prototype.concat(...arrays)
are accepted but apparently provide no type safety.)
Expected behavior: [[17], ["foo"]].flat()
should be typed (number | string)[]
(if it’s accepted at all), and Array.prototype.concat([17], [19], [21])
should be typed number[]
. In both cases the assignment to boolean[]
should be rejected.
Actual behavior: TypeScript infers any[]
for both right hand sides (even with --noImplicitAny
implied by --strict
), and accepts the assignments to boolean[]
with no complaints.
Playground Link: Playground doesn’t support esnext
, but here’s the second line.
Related Issues: #19535 for concat
(but that one may have more to do with the difficulty of typing concat
’s weird behavior when non-arrays are passed?), #26976