as const
The TypeScript suffix as const, which tells the compiler to treat an object's values as fixed literal values rather than general types.
In more detail
An object marked as const keeps every literal exactly instead of widening it to string or number, and all its properties become readonly. It only affects types; use Object.freeze for immutability at run time.
Examples
export const tokens = { colorPrimary: "#2563eb" } as const; types colorPrimary as exactly "#2563eb", so editors autocomplete token names and reject misspelled ones.all: ["users"] as constconst API_CONFIG = { retries: 3, environment: "production" } as const;Related terms
- TypeScript — JavaScript with added type annotations, checked by a compiler before the code runs.
- token formatter — A Style Dictionary step that takes all transformed tokens at once and writes them out as one file in a target language.
- tuple — An ordered, fixed-length group of values in which each position can have its own meaning and type.
- query key — The array that names a piece of cached server data in TanStack Query, such as ["users", "detail", "10"].
- literal type — A type that allows exactly one value, such as the string "GET", the number 404 or true.
- type widening — The compiler giving a value a broader type than its exact value, such as string instead of "production", because it might change later.
- readonly — A modifier, in languages such as C# and TypeScript, that lets a field or property be assigned only when it is initialised, never afterwards.
- satisfies — An operator, value satisfies Type, that checks a value matches a type without changing the value's own, more precise inferred type.
- typeof — A keyword that asks for the type of a value, both in JavaScript at run time and in TypeScript types.
Taught in
- Token Transformation: Style Dictionary & Multi-Platform Outputs — Design Systems & Component Engineering
- Type-Safe Client Generation: Orval & Automated SDKs — Production Full-Stack Engineering
- Primitive Types: String, Number, Boolean, Bigint And Symbol — Production TypeScript
- Literal Types: String, Number, Boolean Literals And const — Production TypeScript
- never: The Bottom Type And Exhaustive Switch Checking — Production TypeScript
- Const Type Parameters: Retaining Literal Types In Functions — Production TypeScript
- The satisfies Operator: Safe Validation Without Widening — Production TypeScript