Bug Report
🔎 Search Terms
- noUncheckedIndexedAccess
- for...of
🕗 Version & Regression Information
⏯ Playground Link
Playground Link
💻 Code
// this example requires "noUncheckedIndexedAccess" to be enabled to make sense
function someNumberFunction(_: number) {}
const myArray: number[] = []
myArray[5] = 5
for(let i = 0; i < myArray.length; ++i) {
const v = myArray[i] // v is "number | undefined"...
if (v != null) { // ... so it needs to be checked for undefined
someNumberFunction(v)
}
}
for(const v of myArray) {
// v is "number" according to TypeScript...
// ... so we can erroneously call the function without checking for undefined
someNumberFunction(v)
}
🙁 Actual behavior
With noUncheckedIndexedAccess enabled, value v in the for...of loop is still treated as number, allowing to unwittingly further process an undefined value.
🙂 Expected behavior
Value v should be treated as number | undefined to be consistent with index access on arrays.
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground Link
💻 Code
🙁 Actual behavior
With
noUncheckedIndexedAccessenabled, valuevin thefor...ofloop is still treated asnumber, allowing to unwittingly further process anundefinedvalue.🙂 Expected behavior
Value
vshould be treated asnumber | undefinedto be consistent with index access on arrays.