-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Open
Labels
ADVANCEDAdvanced CheatsheetAdvanced Cheatsheet
Description
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
Labels
ADVANCEDAdvanced CheatsheetAdvanced Cheatsheet