microsoft / microsoft/TypeScript
Narrowing by type predicate fails to produce intersection type with weak type
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
- predicate
- intersection
- weak
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
⏯ Playground Link
💻 Code
interface A {
label?: string;
a?: string; // make required to eliminate error
}
interface B {
label?: string; // remove shared property to eliminate error
b: boolean;
}
function isB(thing: object): thing is B {
return 'b' in thing;
}
function test(thing: A) {
thing.a; // ok
if (isB(thing)) {
thing.a; // error?
}
}
🙁 Actual behavior
The type of thing inside the conditional is B.
🙂 Expected behavior
The type of thing inside the conditional should be A & B.
Additional information about the issue
This only happens if:
Ais a "weak" type (all optional properties).AandBhave at least one overlapping property.
If either of these isn't the case, then you get the expected intersection type:
This came up in vscode while testing https://github.com/microsoft/TypeScript/pull/58495#issuecomment-2105358748. The issue is there already but is latent because control flow analysis can't see that a type predicate becomes a type assertion without that PR.
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
Reproduce the behavior from the linked TypeScript Playground using the A, B, and isB example. Start by tracing type-predicate narrowing and control-flow analysis in the TypeScript compiler; the issue also points to the related VS Code call site in src/vs/workbench/api/common/extHostTreeViews.ts. Done means the narrowed value retains the expected A & B properties for this weak-type case.
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