Closed
Description
openedon Jun 22, 2024
π Search Terms
"array", "noUncheckedIndexedAccess", "5.5"
π Version & Regression Information
- This changed between versions 5.4.5 and 5.5.2
β― Playground Link
π» Code
const a: number[] = [1, 2, 3];
let i: number = 1;
if(typeof a[i] === "number")
a[i]++
π Actual behavior
Typescript reports the following error:
Object is possibly 'undefined'.(2532)
const a: number[]
π Expected behavior
It should not report an error for such cases
Additional information about the issue
This obviously requires noUncheckedIndexedAccess
to be enabled in the tsconfig.
This only happens when the variable used to access the array is non const (let
in this case).
I think that TS 5.5 actually fixed the original behavior because in 5.4 if we remove the check:
const a: number[] = [1, 2, 3];
let i: number = 1;
a[i]++
TS does not raise the same error (which would be expected as a[i]
could be undefined).
The current workaround:
const j = i;
if (typeof digits[j] === "number") {
digits[j]++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment