Closed
Description
Bug Report
Using a for...in
loop with a constant key variable errors with noUncheckedIndexAccess
.
🔎 Search Terms
for...in
noUncheckedIndexAccess
for(const in)
🕗 Version & Regression Information
Seen in v4.1.5 (oldest playground with noUncheckedIndexAccess) and next (v4.4.0-dev.20210723 at time of writing)
⏯ Playground Link
Playground link with relevant code
💻 Code
type A = {[key: string]: number};
declare var a: A;
for (const key in a) {
const value: number = a[key];
}
🙁 Actual behavior
Error:
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.(2322)
🙂 Expected behavior
No error, key is in object by the for...in
loop and clearly isn't modified as variable is const
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
RyanCavanaugh commentedon Jul 23, 2021
This is the documented behavior of the flag; see #39560. It's entirely possible to read an
undefined
out of this loop if mutation were to occur to the object prior to the read.typescript-bot commentedon Jul 29, 2021
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.