-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
Effort: DifficultGood luck.Good luck.Help WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
Inspired by #10421
The in operator could trivially be seen as a type guard:
interface A {
x: number;
}
interface B {
y: string;
}
let q: A | B = ...;
if ('x' in q) {
// q: A
} else {
// q: B
}Basically, for a n in x where n is a string literal or string literal type and x is a union type, the "true" arm narrows to types which have an optional or required property n, and the "false" arm narrows to types which have an optional or missing property n.
yortus, acrazing, nippur72, yahiko00, aravindarun and 44 morejeffreymorlan, demensky and ethanresnick
Metadata
Metadata
Assignees
Labels
Effort: DifficultGood luck.Good luck.Help WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript