Types can describe a small set of allowed values rather than every possible string or number.
Union types
TypeScript
type Status = "idle" | "loading" | "complete"; let status: Status = "idle";
A union uses |. It is a better model than a broad string when only selected states are valid.
Narrowing
When a value can have several types, TypeScript needs evidence before allowing an operation that works on only one of them.
TypeScript
function display(value: string | number) { if (typeof value === "string") { return value.toUpperCase(); } return value.toFixed(2); }
Did you know my courses are all printer-friendly? This means you can easily export them to PDF or print them for later use.
