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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

value as const

Examples

Literal types from arraysTry it yourself
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
Readonly objectsTry it yourself
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

VersionChange
TypeScript 3.4Added

Was this page helpful?