Caffeinated Coder
Book an appointment
Lesson 06 · Core type system

Unions and narrowing

A union type such as string | number says a value can be one of several types. Follow a seven-line function as tsc refuses a method that only strings have, then watch a typeof check split the union in two: string inside the if, number after it. Finally run the program, see "ab" and 7 take the same two paths and print AB 7.0, and learn why the check survives when the types are erased.

10 min8 steps5 questionsIntermediate
Fig 6 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.