Microsoft/TypeScript
GitHub ã§èŠã'in' does not remove undefined from indexed value (with noUncheckedIndexedAccess)
Open
#61,389 opened on 2025幎3æ10æ¥
Domain: check: Control FlowHelp WantedPossible Improvement
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
in operator, noUncheckedIndexedAccess
ð Version & Regression Information
Tested 5.8.2 and 5.9.0-dev.20250310
⯠Playground Link
ð» Code
With an array:
const arr: number[] = [1, 2, 3];
const idx: number = 2;
if (idx in arr) {
const x: number = arr[idx];
console.log(x);
}
With a map/object:
const map: Record<string, number> = { a: 1 };
const key: string = "a";
if (key in map) {
const x: number = map[key];
console.log(x);
}
ð Actual behavior
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
ð Expected behavior
Expected 'in' operator to guarantee that the result of indexing is not undefined in this branch.
Additional information about the issue
No response