Open SLaks opened 4 years ago
The real-life example (passing a union to a function accepting a union of arrays) appears to be surprisingly hard to do at all.
So far, I've found just one workaround, but it isn't compatible with exhaustiveness checking :(
We have some logic like this around object literals; it should work on array literals too.
For an array the error makes sense because the length is unspecified. For a writable 1-tuple is also makes sense. It is only the special case of a readonly 1-tuple where you should be able to do that conversion.
So this is it
declare const x: Date | Error;
const y: readonly [Date] | readonly [Error] = [x] as const;
// ~ should not be an error on y
Corner case.
TypeScript Version: v4.0.0-dev.20200505, v3.8.3
Search Terms: array union literal
Code
Expected behavior:
No errors.
Array literals of unions should be contextually typed by unions of array literals.
I would also expect
[Date | Error]
to be convertible toDate[] | Error[]
, which also failsActual behavior:
Playground Link: Link
Motivating real-life(-ish) example: Link