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
{{ message }}
Repository navigation
Tuple with optional element and exactOptionalPropertyTypes still returns undefined during iteration #54302
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).
Activity