microsoft / microsoft/TypeScript
Spread operator mixed with destructuring assignment resulting in unsoundness
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
This might have been covered before but I am not sure how to best search for it, apologies if it is a duplicate - when destructuring assignment is used with a spread operator (see example below) TS seems to ignore the possibility that the variable could be assigned undefined.
🔎 Search Terms
spread, destructure, destructuring
🕗 Version & Regression Information
- This is the behavior in every version I tried (since 4.1.5 which is the oldest version on the playground with
noUncheckedIndexedAccessavailable)
⏯ Playground Link
Playground link with relevant code
💻 Code
const [a] = [...(false ? (["a"] as const) : [])];
// ^?
const b = false ? (["a"] as const) : [];
// ^?
const c = [...b];
// ^?
const [d] = c;
// ^?
const [a_prime] = [...(false ? (["a"] as const) : [] as const)];
// ^?
🙁 Actual behavior
a is typed as "a", while d and a_prime are typed as "a" | undefined". Note this only happens when noUncheckedIndexedAccess is enabled, otherwise all of them are typed as "a".
Note that d is the unrolled version of a which makes it more surprising that TS is typing them differently.
🙂 Expected behavior
They should all be typed "a" | undefined.
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 compare the inferred types of a, d, and a_prime with noUncheckedIndexedAccess enabled. Trace the compiler's handling of array destructuring and spread expressions; done means all three cases consistently infer "a" | undefined, with regression coverage added for the reported example.
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