What are Literal Types in TypeScript?
Learn TypeScript literal types: how exact string, number, and boolean values plus unions and as const give compile-time safety and precise autocomplete.
Expected Interview Answer
Literal types let a variable hold one exact value rather than a whole category, so a type can be the specific string 'GET', the number 200, or the boolean true instead of the broad string, number, or boolean.
Combined with union types, literal types create a fixed set of allowed values — for example type Direction = 'up' | 'down' — giving compile-time safety and precise autocomplete. TypeScript widens literals to their base type unless you use const or an as const assertion to keep them narrow. They power discriminated unions, exhaustive switch checks, and API contracts where only certain string or numeric values are valid.
- Restrict a value to an exact allowed set
- Catch typos and invalid states at compile time
- Enable precise autocomplete for allowed values
- Power discriminated unions and exhaustive checks
- Self-document valid API and configuration options
AI Mentor Explanation
A literal type is like a batting order slot that accepts only 'opener', 'middle-order', or 'tailender' — not any word at all. Write 'openner' by mistake and the scorer rejects it instantly. The value must be one exact, pre-agreed role, so no invalid position can ever be entered onto the team sheet.
Step-by-Step Explanation
Step 1
Declare an exact value
Type a variable as a specific literal, e.g. let method: 'GET' allows only that string.
Step 2
Build a union
Combine literals with | to form a fixed allowed set: 'GET' | 'POST' | 'PUT'.
Step 3
Understand widening
A let with a literal widens to its base type; const keeps it as the exact literal.
Step 4
Use as const
Apply as const to objects/arrays to freeze their properties into literal types.
Step 5
Leverage narrowing
Switch or compare on the literal to get exhaustive, type-safe branching.
What Interviewer Expects
- Defines a literal type as one exact value
- Combines literals with union types for allowed sets
- Explains literal widening and how const prevents it
- Knows the as const assertion and its effect
- Connects literals to discriminated unions and exhaustiveness
Common Mistakes
- Confusing literal types with the broad string or number types
- Forgetting that let widens a literal to its base type
- Not using as const when freezing object literals
- Assuming literals exist at runtime rather than compile time
- Overlooking exhaustiveness checks in switch statements
Best Answer (HR Friendly)
“A literal type means a value can only be one specific thing, like the exact word 'admin' rather than any string. By combining a few of these with the OR symbol, you create a short list of allowed values, so TypeScript blocks typos and only permits choices you defined.”
Code Example
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
function request(url: string, method: HttpMethod): void {
switch (method) {
case 'GET':
case 'POST':
case 'PUT':
case 'DELETE':
console.log(`${method} ${url}`)
return
default: {
const _exhaustive: never = method // errors if a case is missed
return _exhaustive
}
}
}
request('/users', 'GET') // ok
// request('/users', 'PATCH') // compile error: not in the unionFollow-up Questions
- What is literal type widening and how does const affect it?
- How does as const change the inferred type of an object?
- How do literal types enable discriminated unions?
- What is a template literal type and when is it useful?
- How do you achieve exhaustiveness checking with the never type?
MCQ Practice
1. What does the type 'up' | 'down' represent?
It is a union of literal types, allowing only the exact strings 'up' or 'down'.
2. What happens to a literal assigned to a let variable?
A let binding widens a literal to its base type; use const or as const to keep it narrow.
3. What does as const do to an object literal?
as const makes every property readonly and infers the narrowest literal types.
Flash Cards
What is a literal type? — A type that is one exact value, like 'GET' or 200, rather than the broad string or number.
How do you allow a fixed set of values? — Combine literals with a union: type Dir = 'up' | 'down'.
What is literal widening? — A let variable widens a literal to its base type; const or as const keeps it narrow.
What does as const do? — Freezes an object/array into readonly properties with the narrowest literal types.