Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

in Operator for Narrowing Section #35

Open
karlhorky opened this issue Nov 15, 2019 · 0 comments
Open

in Operator for Narrowing Section #35

karlhorky opened this issue Nov 15, 2019 · 0 comments

Comments

@karlhorky
Copy link

karlhorky commented Nov 15, 2019

Hi, thanks for this awesome new handbook, I like the new way that you're presenting TypeScript here!

Type guards with the in operator are not mentioned in the Narrowing section.

They are mentioned in the current handbook on the Advanced Types page under the Using the in Operator section:


Using the in operator

The in operator now acts as a narrowing expression for types.

For a n in x expression, where n is a string literal or string literal type and x is a union type, the “true” branch narrows to types which have an optional or required property n, and the “false” branch narrows to types which have an optional or missing property n.

function move(pet: Fish | Bird) {
    if ("swim" in pet) {
        return pet.swim();
    }
    return pet.fly();
}

Potentially related to #9 by @gcrev93

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant