microsoft / microsoft/TypeScript
Array literal of union type should be convertible to union of arrays
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: v4.0.0-dev.20200505, v3.8.3
Search Terms: array union literal
Code
declare let x: Date | Error;
const y: Date[] | Error[] = [x];
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 to Date[] | Error[], which also fails
Actual behavior:
Type '(Date | Error)[]' is not assignable to type 'Date[] | Error[]'.
Type '(Date | Error)[]' is not assignable to type 'Date[]'.
Type 'Date | Error' is not assignable to type 'Date'.
...
Playground Link: Link
Motivating real-life(-ish) example: Link
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the reported assignments in the linked TypeScript Playground examples, using both the array literal and the explicitly typed [Date | Error] case. Trace the compiler's contextual typing and assignability behavior for union and array types; done means both examples type-check without errors while preserving the expected union-of-arrays semantics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100