microsoft / microsoft/TypeScript
Cannot construct optional tuple elements from spreading 0-1 length Array literals
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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
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
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