TypeScript, Lightly

Lesson 2 · puzzle TypeScript, Lightly

Unions & Narrowing

Model states as unions so impossible states don't compile.

Before you start

A discriminated union replaces a bag of optional fields with a set of exact shapes. Instead of { loading?: boolean; data?: T; error?: E } — which permits loading *and* error at once — you enumerate the states that actually exist, and the compiler enforces you handled each.

5 exercises·up to 72 XP·5/5 hearts