Skip to content

[Advanced] error in documentation #787

@johnfn

Description

@johnfn

Type Guarding: Sometimes Union Types solve a problem in one area but create another downstream. If A and B are both object types, A | B isn't "either A or B", it is "A or B or both at once", which causes some confusion if you expected it to be the former. Learn how to write checks, guards, and assertions (also see the Conditional Rendering section below). For example:

This is not true. A | B is indeed A or B. For instance, if type Foo = { a: number } | { b: string }, in a: Foo = { a: 1, b: 'two' }, a would be "both at once" but that value is not assignable to type Foo. It's possible you're thinking of intersection types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ADVANCEDAdvanced Cheatsheet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions