-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do this
Milestone
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 #58495 (comment). 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.
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do this