microsoft / microsoft/TypeScript

Cannot construct optional tuple elements from spreading 0-1 length Array literals

Open
#43,257 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Playground Link

TS 4.0.5, 4.2.2 and 4.3.0-dev20210314

declare let bar: [string, number?];
declare let foo: boolean;
bar = ["", ...[]];
bar = ["", ...[1]];
bar = ["", ...(foo ? [1]: [])];
Type '[string, ...never[]]' is not assignable to type '[string, (number | undefined)?]'.
  Target allows only 2 element(s) but source may have more.(2322)

Type '[string, ...number[]]' is not assignable to type '[string, (number | undefined)?]'.
  Target allows only 2 element(s) but source may have more.(2322)

Type '[string, ...number[]]' is not assignable to type '[string, (number | undefined)?]'.(2322)

Unexpected Errors
Expected ...[] to have no effect when recipient is tuple
Expected ...[1] to resolve into number when recipient is tuple
Expected (foo ? [1]: []) to resolve into a union of the above when recipient is tuple

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

Start with the linked TypeScript Playground example and reproduce the diagnostics for the three tuple assignments across the listed compiler versions. Trace the tuple and spread type-checking behavior, then verify that empty, one-element, and conditional 0–1 element spreads are accepted for the optional tuple element without introducing broader assignment errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.