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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4