microsoft / microsoft/TypeScript

Object access reports error after upgrading to TypeScript 5.5

Open
#59,185 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground repro and compare its diagnostics under 5.4.5 and 5.5.3, focusing on noUncheckedIndexedAccess, nullish assignment, and indexed access. Trace the checker behavior responsible for the regression and add a regression test; done means the example no longer reports the erroneous diagnostic while the relevant safety checks remain intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.