microsoft / microsoft/TypeScript
nested destructured tuple value is `string | undefined` when second tuple item is not accessed
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Bug Report
When using noUncheckedIndexedAccess, the type inferred by my code is affected by whether I access the second item in a tuple or not.
🔎 Search Terms
tuple, undefined, type, omit, noUncheckedIndexedAccess, error, index, array, infer, nested
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
noUncheckedIndexedAccess - I was unable to test this on prior versions because
--noUncheckedIndexedAccessdidn't exist before those versions
⏯ Playground Link
Playground link with relevant code
💻 Code
declare const fn: <T extends string[]>(t: T) => [T, string[]];
const [[x]] = fn(['1']);
console.log(x.toLowerCase());
It seems that the inferred type for fn is:
const fn: <string[]>(t: string[]) => [string[], string[]]
Since the first item is an array rather than a tuple, items in that array have type string | undefined.
If I change the assignment to be:
const [[x], []] = fn(['1']);
It gets inferred as:
const fn: <[string]>(t: [string]) => [[string], string[]]
Since the first item is a tuple rather than an array, items in that tuple have type string.
🙁 Actual behavior
x has type string | undefined
🙂 Expected behavior
x has type string.
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 and reproduce the two destructuring forms under noUncheckedIndexedAccess. Trace the differing inferred types shown in the report; done when the first form gives x the type string while preserving the expected behavior for the second form.
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