microsoft / microsoft/TypeScript
Object access reports error after upgrading to TypeScript 5.5
Nobody has claimed this yet.
- 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
💻 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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