as const
as const is a const assertion. It tells the compiler to infer the narrowest literal types and to treat properties and array elements as readonly.
Detailed Explanation
TypeScript's default inference widens. const status = "ready" is the literal "ready" because const cannot be rebound. let status = "ready" is string, because the binding can change. Object and array members widen even under const: const config = { retries: 3, mode: "live" } has type { retries: number; mode: string }. The literals were forgotten the moment they sat in a property.
as const opts out of that widening:
const config = { retries: 3, mode: "live" } as const;
// { readonly retries: 3; readonly mode: "live" }
Every property becomes readonly. Every array becomes a readonly tuple. Every literal stays a literal. Nested objects are treated the same way, recursively. That single suffix is how you turn a value into a type the rest of the program can key off — discriminants, lookup tables, route lists, enum-like objects.
as const is an assertion, not a cast to a different shape. You cannot write as const on a value that is not a literal context in the way the checker requires — it applies to the expression you attach it to. as const satisfies SomeType is a common pairing: the assertion keeps literals narrow, and satisfies checks the value against a wider contract without widening it.
The usual alternative is an explicit annotation: const config: { retries: 3; mode: "live" } = { ... }. That works and is louder. as const is shorter and keeps the type next to the value. The risk is the same as any assertion: you are asking the compiler to trust the expression's literal form. If the expression is not a literal — if it is a function return, or a spread of a mutable object — you get readonly views of whatever types those parts already had, not new literals.
as const on an array is the difference between string[] and a tuple of specific strings. That matters for keyof and for mapped types that iterate the elements. It also matters for function arguments that expect a tuple: useState returns a tuple; a const-asserted pair can match a tuple parameter.
There is no runtime effect. The emitted JavaScript is the object or array you wrote. Immutability is a type-level claim. If you need the value frozen in the engine, call Object.freeze as well.
Code Examples
1. Widening vs const assertion.
// 1. Widening vs const assertion.
const wide = { method: "GET", path: "/health" };
// method: string
const narrow = { method: "GET", path: "/health" } as const;
// method: "GET"
function send(method: "GET" | "POST"): void {}
// send(wide.method);
// Error: string is not assignable to "GET" | "POST".
send(narrow.method); // fine2. Arrays become readonly tuples.
// 2. Arrays become readonly tuples.
const roles = ["admin", "editor", "viewer"] as const;
type Role = (typeof roles)[number]; // "admin" | "editor" | "viewer"
function canDelete(role: Role): boolean {
return role === "admin";
}
canDelete(roles[0]); // true3. Pair with satisfies so the object stays narrow and still checked.
// 3. Pair with satisfies so the object stays narrow and still checked.
type HeaderMap = Record<string, string>;
const headers = {
Accept: "application/json",
Authorization: "Bearer token",
} as const satisfies HeaderMap;
type HeaderName = keyof typeof headers; // "Accept" | "Authorization"Common Gotchas
`as const` does not freeze at runtime
another reference can still mutate the object. The tuple is `readonly` only through types.
Spreading a const-asserted object into a mutable target loses the literals
`{ ...narrow }` is a new object whose types may widen again unless you re-assert or annotate.
`as const` on a non-literal gives `readonly` of the existing type
`as const` after `JSON.parse(...)` does not invent literal keys. The input has to be a literal expression.
Enums and `as const` objects overlap
a const-asserted object plus `typeof obj[keyof typeof obj]` usually replaces a string enum with less runtime code. Do not maintain both for the same set of values.