TypeScript Types
Literal types — exact value constraints
A literal type represents a particular primitive value, rather than its entire primitive category. Literal unions are useful for closed sets of accepted options. Inference may widen a mutable variable or an object property to string or number; explicit annotations and const assertions can preserve precision when needed. These constraints do not validate external data at runtime. Example output: "dark". The example was designed for strict checking with an ES2022 target; type annotations do not add runtime validation.
Formal Syntax & Structure
typescript syntaxtypescript
type Mode = "light" | "dark";Working Example
Example Snippettypescript
type Mode = "light" | "dark";const mode: Mode = "dark";console.log(mode);