const assertions
as const
as const freezes a value into its narrowest literal type. The string north stops widening to string.
Dir is the single value north, so only that exact string is accepted. Use it for fixed choices with one option.
const dir = "north" as const; type Dir = typeof dir; const d: Dir = "north"; console.log(d);
north
Tuple inference
Arrays get the tuple treatment plus readonly in one step. Length and element types are locked together.
Rgb is a readonly triple of 255, 0, and 0. Push or reassignment fails at compile time.
const rgb = [255, 0, 0] as const; type Rgb = typeof rgb; const first: Rgb[0] = 255; console.log(first, rgb[0]);
255 255
Frozen objects
Objects become deeply readonly with literal values. Config data can be shared without fear of mutation.
host stays the literal localhost and port stays 8080. Any write is rejected before the code ever runs.
const config = { host: "localhost", port: 8080, } as const; console.log(config.port);
8080
Instead of enums
A const object plus indexed access replaces many enums. Keys give readable names while values stay plain strings.
Role is admin or guest with zero runtime cost beyond the object itself. Add a member once and every consumer sees it.
const Roles = { Admin: "admin", Guest: "guest", } as const; type Role = (typeof Roles)[keyof typeof Roles]; const me: Role = "admin"; console.log(me, Roles.Admin, Roles.Guest);
admin admin guest
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
as constdo to a value?Answer
It keeps the narrowest literal type of the value
The assertion stops widening, so a literal stays its exact literal type.
-
What is the type of
[255, 0, 0] as const?Answer
readonly [255, 0, 0]Each element keeps its literal and the whole tuple becomes readonly.
-
What does
as constdo to an object literal?Answer
It makes every property deeply readonly with a literal type
The assertion applies to the object and every nested value, so writes are refused.
-
What does a const object plus indexed access replace?
Answer
Many enums, with readable names and plain string values
The object supplies names and values, and the indexed type is the union of the values.
-
What does this program print?
const dir = "north" as const; type Dir = typeof dir; const d: Dir = "north"; console.log(d);Answer
north
The constant holds the frozen literal, and the log prints it.
-
What does this program print?
const rgb = [255, 0, 0] as const; type Rgb = typeof rgb; const first: Rgb[0] = 255; console.log(first, rgb[0]);Answer
255 255
The first element type is the literal 255, and the array still holds 255 there.
-
What does this program print?
const config = { host: "localhost", port: 8080, } as const; console.log(config.port);Answer
8080
Readonly forbids writes, not reads, so the port value prints.
-
What does this program print?
const Roles = { Admin: "admin", Guest: "guest", } as const; type Role = (typeof Roles)[keyof typeof Roles]; const me: Role = "admin"; console.log(me, Roles.Admin, Roles.Guest);Answer
admin admin guest
The constant holds admin, and the two object fields hold admin and guest.
-
What does the compiler say about this program?
const rgb = [255, 0, 0] as const; rgb.push(1); console.log(rgb);Answer
error TS2339: Property 'push' does not exist on type 'readonly [255, 0, 0]'.
A readonly tuple has no mutating methods, so push is not part of its type.
-
What does the compiler say about this program?
const dir = "north" as const; type Dir = typeof dir; const d: Dir = "south"; console.log(d);Answer
error TS2322: Type '"south"' is not assignable to type '"north"'.
Dir is the single literal north, so south does not fit it.
Article author: Arthur Isaev