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

Exhaustiveness Checking with never

Flashcard
20

Narrowing Out null and undefined

Flashcard
21

Assertion Functions (asserts)

Flashcard
22

Control Flow Analysis Narrowing

Flashcard
23

Non-Null Assertion Operator (!)

Flashcard
24

in Operator Narrowing

Flashcard
25

User-Defined Type Guards (is)

Flashcard
26

Truthiness Narrowing

Flashcard
27

Discriminated Unions

Flashcard
28

typeof Type Guards

Flashcard
29

Narrowing the unknown Type

Flashcard
30

instanceof Type Guards

Flashcard
31

Array.isArray Narrowing

Flashcard
32

Type Assertions vs Type Guards

Flashcard
33

Filter Callback Missing Type Predicate

Code Quiz
34

Helper Function Loses Narrowing

Code Quiz
35

Optional Chaining Yields Possibly Undefined

Code Quiz
36

Wrong Null Check Misses Undefined

Code Quiz
37

Using unknown Without Narrowing

Code Quiz
38

typeof Cannot Detect Arrays

Code Quiz
39

Equality Narrowing Dead Branch

Code Quiz
40

Non-null Assertion Hides Real Bug

Code Quiz
41

Truthiness Narrowing Drops Zero

Code Quiz
42

Reassignment Resets Narrowing

Code Quiz
43

Truthiness Narrowing

Quiz
44

Type Predicate Return Effect

Quiz
45

Array.isArray as Type Guard

Quiz
46

never in Exhaustiveness Checking

Quiz
47

Narrowing via Assignment

Quiz
48

Non-null Assertion Operator

Quiz
49

Equality Narrowing

Quiz
50

Narrowing Literal and Union Types

Quiz
51

Narrowing Loss in Closures

Quiz
52

typeof Guards for Primitives

Quiz
53

Control Flow Analysis Basics

Quiz
Code Quiz

Discriminated Union Narrowing in React

Spot the property access bug inside a narrowed branch of a discriminated union.

Codetypescript
type LoadingState = { status: "loading" };
type SuccessState = { status: "success"; data: string };
type ErrorState = { status: "error"; message: string };

type State = LoadingState | SuccessState | ErrorState;

function Display({ state }: { state: State }) {
  if (state.status === "loading") {
    return <p>Loading...</p>;
  } else if (state.status === "success") {
    return <p>{state.message}</p>;
  } else {
    return <p>{state.message}</p>;
  }
}

The discriminated union is correctly defined, but this component fails to compile. What is the bug?