Literal Types
| Since: | TypeScript 1.0(2014) |
|---|
A feature that treats specific values themselves as types. By using concrete string or numeric values as types, you can strictly restrict the allowed values. Combined with as const, you can lock an entire object or array into literal types.
Syntax
let variableName: "value" = "value";
// Numeric literal type
let variableName: 1 | 2 | 3 = 1;
// as const: fixes the value as a literal type.
const variableName = "value" as const; // Type becomes "value" (not string).
const obj = { key: "value" } as const; // All properties become readonly and literal types.
Syntax Overview
| Syntax | Description |
|---|---|
| "value" (string literal type) | A type that only accepts a specific string value. Used with union types to enumerate a set of allowed options. |
| Numeric literal type | A type that only accepts a specific number. Useful for restricting values like dice rolls or status codes. |
| true / false (boolean literal type) | A type fixed to either true or false, but not both. |
| as const | Fixes a variable's type as a literal type without widening. Can also be applied to objects and arrays. |
| typeof obj[keyof typeof obj] | An idiom for extracting the value types of an as const object as a union type. |
Sample Code
type Direction = "north" | "south" | "east" | "west";
let move: Direction = "north";
// move = "up"; // Error: "up" is not assignable to type Direction.
// Numeric literal type: only values 1 through 6 are allowed.
type DiceValue = 1 | 2 | 3 | 4 | 5 | 6;
let roll: DiceValue = 3;
// roll = 7; // Error: 7 is not assignable to type DiceValue.
// Using a literal type for a function parameter.
function setTheme(theme: "light" | "dark"): void {
console.log(`Switched theme to ${theme}`);
}
setTheme("dark"); // Works fine.
// setTheme("blue"); // Error: "blue" is not an allowed value.
// as const: fixes the variable type as a literal type.
const status = "active" as const; // Type is "active", not string.
// let status = "active"; // This would be inferred as type string.
// Using as const on an object.
const Config = {
host: "localhost",
port: 3000,
mode: "development",
} as const;
// Config.port = 8080; // Error: cannot assign to a readonly property.
console.log(Config.host); // Outputs "localhost".
// Extracting value types from an as const object.
const INSPECTORS = {
Division1: "kogami",
Division2: "tsunemori",
Division3: "ginoza",
} as const;
type Inspector = typeof INSPECTORS[keyof typeof INSPECTORS]; // "kogami" | "tsunemori" | "ginoza"
let lead: Inspector = "kogami";
console.log(lead); // Outputs "kogami".
Running the above produces the following output:
npx ts-node ts_literal_type.ts Switched theme to dark localhost kogami
Details
Literal types let you use "a specific value itself" as a type. They apply to strings, numbers, and booleans. Combined with union types, you can express types like "one of these three strings." This lets you strictly restrict the values passed to a function and catch typos at compile time.
The as const assertion fixes type inference to the "narrowest (most specific) type." Normally, writing let x = "hello" causes TypeScript to infer the type as string (a wide type), but const x = "hello" as const makes the type "hello" (a literal type). Applied to an object, all properties become readonly and literal types.
as const is sometimes used as an alternative to enum. While an enum generates a JavaScript object after compilation, an as const object remains as-is, which can be advantageous in terms of bundle size.
If you find any errors or copyright issues, please contact us.