microsoft / microsoft/TypeScript

Array literal of union type should be convertible to union of arrays

Open
#38,380 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Variance Relationships
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.