You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
typeItem={value: string;};typeFoo=[Item?];letfoo1: Foo=[];// no error, good, there can be 0 or 1 itemletfoo2: Foo=[undefined];// error, good, because of `exactOptionalPropertyTypes: true`; if I turn this option off, the error goes awayletfoo3: Foo=[{value: '1'}];// no error, good, obviouslyconsole.log('1');for(letitemoffoo1){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 itemconsole.log('4');// this isn't logged because of runtime error on previous linetypeBar=[Item];letbar: Bar=[{value: '1'}];for(letitemofbar){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).
The text was updated successfully, but these errors were encountered:
Bug Report
When iterating over a variable with a tuple type with optional element
[T?]
, the type of the item is stillT | undefined
even whenexactOptionalPropertyTypes: true
(i.e. creating a variable likelet 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
🙁 Actual behavior
Error on line 15 (
console.log(item.value);
) becauseitem
has typeItem | undefined
.🙂 Expected behavior
I would expect no error on line 15 and the type of
item
to beItem
. 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 triedreadonly [Item?]
, although it too doesn't affect anything (as I think it shouldn't).The text was updated successfully, but these errors were encountered: