-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
let
indexes using noUncheckedIndexedAccess in TS 5.5.2
#58972
Comments
let
indexes using noUncheckedIndexedAccess in TS 5.5.2
And also the PR: #57847 This intentionally is only working for constant-like variables. |
This does seem to meet the criteria in #57847 though:
If the initializer counts as an "assignment" in this context then it effectively doesn't work for |
Heh, this is a fun one... in a sense this is surprising. TS thinks Even though it doesn't exactly look like it... this feels like a duplicate of #58345 . Obviously, this repro is wildly different from the other one but the implementation source of the problem is very likely the same |
This is working as intended. We specifically exclude global variables from assignment analysis as the control flow analyzer can't see all references (there might be references in other files). Adding an |
Thanks, for the clarifications, I was simply surprised because the behavior in a very "hello world" code is now a bit confusing for non-experienced people: function goThroughArray() {
const a: number[] = [1, 2, 3];
for(let i = 0; i < a.length; i++) {
if (typeof a[i] === "number") {
a[i]++; // a[i] can be undefined
}
}
} Which is not exactly the reproducer I have send in the original issue, as I was trying to narrow this down. However given that the compiler can't know if |
Ah, shoot. I forgot that those are still global variables (even if not available on
This would be a duplicate of #58803 . CFA graph walk could likely check if an assignment happens between those 2 points but that could cost some perf. |
π Search Terms
"array", "noUncheckedIndexedAccess", "5.5"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&noUnusedLocals=true&noUnusedParameters=true&ts=5.5.2#code/MYewdgzgLgBAhgLhmArgWwEYFMBOBtAXRgF4Y8BGAGhgCZqBmAgbgCgAbLWASyVU1xIxyrFlwBmACigBPAA5YQY+Hi5Fi6mACI+2HJoCULGMeWqA1GaA
π» Code
π 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:
TS does not raise the same error (which would be expected as
a[i]
could be undefined).The current workaround:
The text was updated successfully, but these errors were encountered: