-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Open
Labels
Domain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
π 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
Metadata
Metadata
Assignees
Labels
Domain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases