Skip to content

TypeScript Union Types, Literal Types and Narrowing: typeof, in and Discriminated Unions

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Real values are often one of a few possibilities. An amount may arrive as a number or as text. An order status is pending, paid or delivered, and nothing else. A payment is by card, cash or wallet, and each kind carries different details. TypeScript describes these with union types, written with a vertical bar, and literal types, which allow only exact values. Then it does something clever: inside an if or a switch that checks which possibility you have, it narrows the type, so you can use exactly the properties that exist in that case. This lesson is where TypeScript starts to feel less like extra typing and more like a colleague who never forgets an edge case.

Quick answer

A union type lists alternatives with |: string | number means either. A literal type allows exact values: type Currency = "AED" | "SAR" | "QAR". Before you use a union, narrow it with a check TypeScript understands: typeof x === "string", a truthiness check such as if (name), "phone" in contact, Array.isArray(list) or a comparison. For objects, give each case a shared property with a literal value, such as kind: "card", and switch on it; this is a discriminated union. Add a default that assigns the value to never so the compiler tells you when a new case is not handled.

35 min read and practice · Content checked on 17 November 2026

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.

Try it: An amount that may be text or a number
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.