TypeScript 7 reference · Type Operators and Keywords
TypeScript as const Keyword
Make a value deeply read-only with literal types: strings stay 'admin' instead of string, arrays become readonly tuples.
TypeScript 7 Since 3.4
Syntax
value as const
Examples
const plain = ["GET", "POST"]; // string[]
const exact = ["GET", "POST"] as const; // readonly ["GET", "POST"]
type Method = (typeof exact)[number]; // "GET" | "POST"
const m: Method = "GET";
console.log(plain.length, exact.length, m);
Output
2 2 GET
const settings = { mode: "dark", size: 14 } as const;
// @ts-expect-error: as const makes properties readonly
settings.mode = "light";
console.log(settings);
Output
{ mode: 'light', size: 14 }Version history
| Version | Change |
|---|---|
| TypeScript 3.4 | Added |
Related
satisfies· Check that a value matches a type without widening it, so you keep the exact inferred type and still get errors.Readonly<Type>· Make every property of a type read-only, so the compiler rejects assignments to them after creation.typeof (type query)· In a type position, typeof takes the type of a value or function, so you don't have to write it twice.- TypeScript keyof, typeof and Indexed Access · tutorial