Explore Library
TypeScriptType Narrowing & Type Guards

53 items

1

Discriminated Union Narrowing in React

Code Quiz
2

Discriminated Unions in React Props

Quiz
3

Narrowing Invalidation

Flashcard
4

Type Guards vs Type Assertions

Slides / Video
5

Narrowing the unknown Type

Slides / Video
6

Control Flow Analysis and Narrowing Scope

Slides / Video
7

Assertion Functions with 'asserts'

Slides / Video
8

Exhaustiveness Checking with never

Slides / Video
9

Discriminated Unions and Tagged Narrowing

Slides / Video
10

User-Defined Type Guards with 'is'

Slides / Video
11

Null and Undefined Narrowing

Slides / Video
12

Equality Narrowing with === and switch

Slides / Video
13

Truthiness Narrowing

Slides / Video
14

The in Operator for Property Checking

Slides / Video
15

instanceof Type Guards

Slides / Video
16

typeof Type Guards

Slides / Video
17

What Type Narrowing Is

Slides / Video
18

Equality Narrowing

Flashcard
19

User-Defined Type Guards (is)

Flashcard
20

typeof Type Guards

Flashcard
21

Array.isArray Narrowing

Flashcard
22

Type Assertions vs Type Guards

Flashcard
23

Non-Null Assertion Operator (!)

Flashcard
24

Assertion Functions (asserts)

Flashcard
25

in Operator Narrowing

Flashcard
26

Control Flow Analysis Narrowing

Flashcard
27

Discriminated Unions

Flashcard
28

instanceof Type Guards

Flashcard
29

Exhaustiveness Checking with never

Flashcard
30

Narrowing Out null and undefined

Flashcard
31

Narrowing the unknown Type

Flashcard
32

Truthiness Narrowing

Flashcard
33

Helper Function Loses Narrowing

Code Quiz
34

Filter Callback Missing Type Predicate

Code Quiz
35

typeof Cannot Detect Arrays

Code Quiz
36

Non-null Assertion Hides Real Bug

Code Quiz
37

Wrong Null Check Misses Undefined

Code Quiz
38

Equality Narrowing Dead Branch

Code Quiz
39

Truthiness Narrowing Drops Zero

Code Quiz
40

Optional Chaining Yields Possibly Undefined

Code Quiz
41

Using unknown Without Narrowing

Code Quiz
42

Reassignment Resets Narrowing

Code Quiz
43

Non-null Assertion Operator

Quiz
44

typeof Guards for Primitives

Quiz
45

Array.isArray as Type Guard

Quiz
46

Narrowing Loss in Closures

Quiz
47

Narrowing Literal and Union Types

Quiz
48

never in Exhaustiveness Checking

Quiz
49

Narrowing via Assignment

Quiz
50

Type Predicate Return Effect

Quiz
51

Control Flow Analysis Basics

Quiz
52

Equality Narrowing

Quiz
53

Truthiness Narrowing

Quiz
Quiz

Discriminated Unions in React Props

Understanding how discriminated unions enable exhaustive type narrowing when typing React component props.

You type a Button's props as a discriminated union to enforce that only one of two shapes is valid: type ButtonProps = | { variant: 'link'; href: string } | { variant: 'action'; onClick: () => void }; Inside the component you write: function Button(props: ButtonProps) { if (props.variant === 'link') { return <a href={props.href}>Go</a>; } return <button onClick={props.onClick}>Click</button>; } Which statement about this code is TRUE?