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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy