In this lesson you will
- Write union types and literal types for values with a few possible forms
- Narrow unions with typeof, truthiness, in, Array.isArray and equality
- Model cases with discriminated unions and switch on them
- Use never to make the compiler find unhandled cases
Union types and typeof narrowing
Write string | number and read it as "a string or a number". A function that accepts an amount typed by a person or calculated by code might take amount: string | number. TypeScript only lets you do things that work for every member of the union, so amount.toFixed(2) is an error: strings have no toFixed. That is exactly the bug the union protects you from. To use number methods, first check: if (typeof amount === "string") { ... }. Inside that block TypeScript treats amount as a string; after it, or in the else, it knows only a number is left.
This is called narrowing, and TypeScript follows your code to do it. A return inside the if narrows the rest of the function too. typeof works for "string", "number", "boolean", "bigint", "symbol", "undefined", "function" and "object"; remember that typeof null is "object", which is why a separate null check is often needed. A union with null or undefined, such as string | null, is narrowed with a simple truthiness check, if (name), but be careful: that also rejects an empty string and, for numbers, 0.
Console
- Console messages, errors, form data and clicked links appear here.
Both branches use methods that only exist on one member of the union, and both are allowed because the typeof check narrowed the type first. In greet, the early return narrows name to a string for the rest of the function.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between a union type and an enum in TypeScript?
A literal union such as "AED" | "SAR" exists only as a type and is erased, while an enum also creates a JavaScript object at run time. Many teams now prefer literal unions, often built from an as const array, because they are simpler and match plain strings in JSON.
What does as const do in TypeScript?
It tells TypeScript to keep the most exact types for a value: strings stay as their literal values instead of widening to string, and arrays and objects become readonly. It has no effect on the JavaScript output.
What is the never type used for?
never is the type with no values. It appears when narrowing has removed every possibility, and as the return type of a function that always throws. Its most practical use is the exhaustive check at the end of a switch over a union.
What is a type guard in TypeScript?
Any check TypeScript uses to narrow a type, such as typeof, in or Array.isArray. You can also write your own: a function with a return type like value is Order tells TypeScript that when it returns true, the value is an Order.
Can TypeScript check JSON from an API at run time?
No, types are erased before the code runs. Type the parsed value as unknown and narrow it with checks, or use a validation library such as Zod that checks the data at run time and gives you the matching type.
Sources
Facts in this lesson were checked against these official pages on 17 November 2026.
- TypeScript Handbook: Everyday Types (union and literal types)
- TypeScript Handbook: Narrowing
- TypeScript Handbook: Typeof Type Operator
- TypeScript Handbook: Enums
- MDN: typeof
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.