Closed
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
.