-
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
Object access reports error after upgrading to TypeScript 5.5 #59185
Comments
The change between v5.4.5 and main occurred at 247a983. |
I highly doubt that, @typescript-bot |
5.4 incorrectly always allowed let str: string | undefined;
if (Math.random() < 0.1) {
str = "a"
}
const acc: { [name: string]: number } = {};
if (str != null) {
// acc[str] ??= 0;
acc[str]++; // no error in 5.4
} This has been fixed in 5.5 and that affects ur repro. TS 5.5 introduces Control Flow Narrowing for Constant Indexed Accesses but we can read there that:
Since your let str = Math.random() < 0.1 ? "a" : undefined;
const acc: { [name: string]: number } = {};
if (str != null) {
acc[str] ??= 0;
acc[str]++;
}
export {}; |
Thanks for some of the context @Andarist! Perhaps that makes this a feature request instead of a bug report then. It does seem like for the duration of these three lines, if (str != null) {
acc[str] ??= 0;
acc[str]++;
} Does that seem reasonable, or am I missing something here? |
From what I understand, it was a performance concern to allow further analysis to handle cases like those. But yes - it is a potential feature request. |
π Search Terms
"noUncheckedIndexedAccess", "Object is possibly 'undefined'."
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.5.3#code/DYUwLgBAzmBOBc04EsB2BzCAfCBXVAJiAGZogEDcAUMsRABQCyAhmABYB0szhA9gLb0AlBAA8EAAwcAjCIDeVCElgQAvBABEzDVQC+VKgGNeqGBGaHDiORADaqZvxCIYsNOgC6iVLn4AjEBVdNQg5XWpaBlcIAEJ1H2BgeUVzS1tXDwgAfiz1CWolC0N0uA8AajLqfSA
π» Code
π Actual behavior
In TS 5.5.x, with
noUncheckedIndexedAccess
enabled,acc[str]++;
reports an error "Object is possibly 'undefined'. (2532)".π Expected behavior
In TS 5.4.x and before, no errors were reported.
Additional information about the issue
I admittedly didn't do a very deep dive on the root cause here, but wanted to file an issue because intuitively it felt unexpected when upgrading to TS 5.5 that the above code now fails type-checking. Apologies if I missed an existing issue for this or something in the release notes!
The text was updated successfully, but these errors were encountered: