microsoft / microsoft/TypeScript
Aliased optional chain calls don't narrow like their inline equivalents
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
aliased conditions control flow analysis cfa optional chain call
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
function resolve1(id: string | undefined) {
const isBundled = id?.includes("_bundled");
if (isBundled) {
const str: string = id; // error
}
}
function resolve2(id: string | undefined) {
if (id?.includes("_bundled")) {
const str: string = id; // ok
}
}
🙁 Actual behavior
resolve1 errors within the if block
🙂 Expected behavior
I'd expect it to be OK. Just like resolve2 is OK with it here - the condition is exactly the same and TS supports Control Flow Analysis of Aliased Conditions and Discriminant since 4.4
Additional information about the issue
No response
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 reproduction and compare resolve1 with resolve2, then trace the control-flow analysis for aliased conditions and optional-chain calls. Add or update a regression test showing that the aliased condition narrows id like the inline condition, and verify that both string assignments succeed.
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
- 45/100