Syntax
let value: "specific-string" | 42 | true;Examples
String and Numeric Literal Unions
Restricting a value to an exact, known set of options.
type Direction = "up" | "down" | "left" | "right";
type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;
function move(direction: Direction) {
console.log(`Moving ${direction}`);
}
move("up"); // OK
// move("north"); // Error - not one of the allowed literalsconst Assertions
Using "as const" to infer the most specific literal type possible.
let a = "hello"; // inferred as: string (widened)
const b = "hello"; // inferred as: "hello" (literal, since const cannot be reassigned)
let config = {
mode: "production"
} as const;
// config.mode is typed as the literal "production", not string
// config.mode = "development"; // Error - as const also makes properties readonlyBest practices
- Use literal type unions instead of a general string/number type when a value only has a small, known set of valid options - it catches typos at compile time
- Use as const on object/array literals to lock in their most specific literal types, especially useful for configuration objects
- Combine literal types with discriminated unions for exhaustive, type-safe handling of different variant shapes
- Prefer literal unions over TypeScript enums for simple cases - they compile to nothing extra and integrate more naturally with plain JavaScript
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Type Inference
TypeScript can automatically determine a value's type from its initializer, without an explicit annotation - this is type inference. It reduces boilerplate significantly while still providing full type safety. Understanding what TypeScript infers (and when it needs help) is key to writing idiomatic, uncluttered TypeScript.
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Type Inference
TypeScript can automatically determine a value's type from its initializer, without an explicit annotation - this is type inference. It reduces boilerplate significantly while still providing full type safety. Understanding what TypeScript infers (and when it needs help) is key to writing idiomatic, uncluttered TypeScript.