Const Assertion
In one line
A const assertion (as const) tells TypeScript to infer the narrowest possible types — literal types instead of widened base types, and readonly arrays/tuples.
In simple words
A const assertion (as const) tells TypeScript to use the most specific types possible. It prevents type widening: 5 stays 5 instead of becoming number, and arrays become readonly tuples.
For example, const colors = ["red", "green"] as const gives colors the type readonly ["red", "green"] — a readonly tuple with literal string types. Without as const, it would be string[].
Const assertions are useful for exhaustiveness checking, defining constant maps, and creating immutable configurations. They work on object literals, arrays, and expressions. They are a compile-time operation with no runtime effect.
Example
const status = "active" as const; // type: "active" (not string)
const directions = ["N", "S", "E", "W"] as const; // type: readonly ["N", "S", "E", "W"]
const config = { port: 3000 } as const; // type: { readonly port: 3000 }as const prevents widening: strings stay literal, arrays become readonly tuples.
Related terms
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.