microsoft / microsoft/TypeScript
Tuple with optional element and exactOptionalPropertyTypes still returns undefined during iteration
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
When iterating over a variable with a tuple type with optional element [T?], the type of the item is still T | undefined even when exactOptionalPropertyTypes: true (i.e. creating a variable like let foo: [string?] = [undefined] errors).
🔎 Search Terms
tuple, optional tuple element, exactOptionalPropertyTypes
🕗 Version & Regression Information
This is the behavior in every relevant version I tried (4.4.4 to 5.1-beta), and I reviewed the FAQ for entries about tuples
⏯ Playground Link
Playground link with relevant code
💻 Code
type Item = {
value: string;
};
type Foo = [Item?];
let foo1: Foo = []; // no error, good, there can be 0 or 1 item
let foo2: Foo = [undefined]; // error, good, because of `exactOptionalPropertyTypes: true`; if I turn this option off, the error goes away
let foo3: Foo = [{value: '1'}]; // no error, good, obviously
console.log('1');
for (let item of foo1) {
console.log('2');
console.log(item.value); // error, but why? `'2'` isn't logged during runtime
}
console.log('3');
console.log(foo1[0].value); // error, good, because there may be no item
console.log('4'); // this isn't logged because of runtime error on previous line
type Bar = [Item];
let bar: Bar = [{value: '1'}];
for (let item of bar) {
console.log(item.value); // no error, good
}
🙁 Actual behavior
Error on line 15 (console.log(item.value);) because item has type Item | undefined.
🙂 Expected behavior
I would expect no error on line 15 and the type of item to be Item. Is this maybe somehow related to #51643? Or is my expectation incorrect and there's some possible runtime behavior that I'm not taking into account? But I think that e.g. noUncheckedIndexedAccess doesn't apply in this situation (and toggling it off and on didn't change anything anyway). I also tried readonly [Item?], although it too doesn't affect anything (as I think it shouldn't).
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
Begin with the linked TypeScript Playground reproduction and trace how the compiler types a for-of loop over an optional tuple element with exactOptionalPropertyTypes enabled. Locate the compiler's regression-test area for tuple iteration, then add coverage for the shown Foo and Bar cases; done means the Foo loop item is treated as Item while the potentially absent indexed access remains rejected.
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
- 45/100