Literal type
A type whose only value is a specific string, number, or boolean.
Syntax
'value' | 42 | true Examples
let dir: 'left' | 'right';
dir = 'left';
// dir = 'up'; // Error
function roll(): 1 | 2 | 3 | 4 | 5 | 6 {
return 4;
}
const ok = true as const; // type: true
Notes
Literal types restrict a value to exact constants and are the foundation of
string-union enums and discriminated unions. `const` declarations infer
literal types automatically; `let` widens to the base primitive unless
`as const` is used.