Skip to content
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

Open
jkillian opened this issue Jul 8, 2024 · 5 comments
Open

Object access reports error after upgrading to TypeScript 5.5 #59185

jkillian opened this issue Jul 8, 2024 · 5 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@jkillian
Copy link

jkillian commented Jul 8, 2024

πŸ”Ž Search Terms

"noUncheckedIndexedAccess", "Object is possibly 'undefined'."

πŸ•— Version & Regression Information

  • This changed between versions 5.4.5 and 5.5.3

⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.5.3#code/DYUwLgBAzmBOBc04EsB2BzCAfCBXVAJiAGZogEDcAUMsRABQCyAhmABYB0szhA9gLb0AlBAA8EAAwcAjCIDeVCElgQAvBABEzDVQC+VKgGNeqGBGaHDiORADaqZvxCIYsNOgC6iVLn4AjEBVdNQg5XWpaBlcIAEJ1H2BgeUVzS1tXDwgAfiz1CWolC0N0uA8AajLqfSA

πŸ’» Code

// @strict: true
let str: string | undefined;
if (Math.random() < 0.1) {
  str = "a"
}

const acc: { [name: string]: number } = {};
if (str != null) {
  acc[str] ??= 0;
  acc[str]++; // In TS 5.5: Object is possibly 'undefined'. (2532)
}

πŸ™ 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!

@typescript-bot
Copy link
Collaborator

The change between v5.4.5 and main occurred at 247a983.

@andrewbranch
Copy link
Member

I highly doubt that, @typescript-bot

@Andarist
Copy link
Contributor

Andarist commented Jul 9, 2024

5.4 incorrectly always allowed acc[str]++:

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:

TypeScript is now able to narrow expressions of the form obj[key] when both obj and key are effectively constant.

Since your str isn't effectively constant... this doesn't work in a form that u'd like it to. Notice how this version works alright:

let str = Math.random() < 0.1 ? "a" : undefined;

const acc: { [name: string]: number } = {};

if (str != null) {
  acc[str] ??= 0;
  acc[str]++;
}

export {};

@jkillian
Copy link
Author

jkillian commented Jul 9, 2024

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, str is effectively constant enough that TS could allow the code regardless of how str is assigned above:

if (str != null) {
  acc[str] ??= 0;
  acc[str]++;
}

Does that seem reasonable, or am I missing something here?

@Andarist
Copy link
Contributor

Andarist commented Jul 9, 2024

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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants