What are literal types in TypeScript?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for TypeScript development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

Literal types restrict a type to a specific set of exact values, rather than any value of that base type. String literals: type Direction = "North" | "South" | "East" | "West"; — only these four strings are valid, not any arbitrary string. Numeric literals: type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;. Boolean literals: type Yes = true;. You can mix literal types in a union: type InputMode = "text" | "password" | 0 | false;. Literal types are crucial for discriminated unions — objects with a common literal property used to distinguish variants: type Shape = { kind: "circle"; radius: number } | { kind: "rect"; width: number; height: number };. TypeScript narrows the type when you check shape.kind. Const assertions (as const) turn widened types into literal types: const colors = ["red", "green", "blue"] as const; — type is readonly ["red", "green", "blue"], not string[].

Common Mistake

A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.