Sub topic 7
What `as const` cannot do
Prerequisites: you should know basic TypeScript types, the difference between let and const, union and literal types, and type operators such as typeof and keyof.
as const is a compile-time feature. It tells TypeScript to keep the narrowest possible literal types and to mark literal object properties or array elements as readonly. It does not change JavaScript values while the program runs.
It only applies to literal expressions
A literal expression directly writes a value such as "admin", 42, an object literal, or an array literal. You can apply a const assertion to these expressions:
const role = "admin" as const;
const limits = { retries: 3 } as const;
const pair = ["id", 1] as const;You cannot apply as const to an arbitrary expression, such as a function call or an existing variable:
declare function readRole(): string;
const literalRole = "admin" as const;
// @ts-expect-error A const assertion cannot be applied to a function call.
const returnedRole = readRole();
let candidate = "admin";
// @ts-expect-error A const assertion cannot be applied to a variable.
const narrowedCandidate = candidate as const;If a value already exists in a variable, use a type annotation, a type guard, or another narrowing technique. as const is not a general-purpose command that freezes or narrows any expression.
It does not freeze values at runtime
The word readonly affects type checking. It does not call Object.freeze, prevent JavaScript mutation, or protect a value from code that uses another reference.
const settings = { retries: 1 } as const;
// The readonly type rejects direct assignment.
// @ts-expect-error Cannot assign to a readonly property.
settings.retries = 2;
// A type assertion can deliberately remove the readonly view.
const mutableSettings = settings as { retries: number };
mutableSettings.retries = 2;
console.log(settings.retries); // 2The program changes the same object through mutableSettings. The original settings variable sees that change because both variables refer to one object.
A const assertion also does not validate data received from a file, network request, or user. External data can have any shape at runtime. Validate it with explicit checks or a runtime validation library before using it as a trusted value.
Readonly does not mean deeply immutable
For nested literals written inside the same expression, TypeScript can infer readonly nested properties:
const localConfig = {
server: {
host: "localhost",
port: 3000,
},
} as const;
// @ts-expect-error The nested literal property is readonly too.
localConfig.server.port = 4000;However, a const assertion does not recursively freeze objects that were created elsewhere. The property reference becomes readonly, but the referenced object may still be mutable through its original name:
const server = {
host: "localhost",
port: 3000,
};
const config = { server } as const;
// @ts-expect-error The server property itself cannot be replaced.
config.server = { host: "example.com", port: 443 };
server.port = 4000;
console.log(config.server.port); // 4000The distinction is important:
| Feature | What it controls | Runtime effect |
|---|---|---|
| `as const` | Inferred literal and readonly types | None |
| `Object.freeze` | Direct writes to an object | Prevents direct writes in supported runtime behavior |
| A copied object | Which object a variable refers to | Creates a separate value, but does not automatically deep-freeze it |
It does not make every assignment impossible
as const narrows a value. It does not make a variable binding itself equivalent to a const declaration.
let status = "active" as const;
// The binding is still declared with let, but its only allowed value is "active".
// @ts-expect-error The literal type is "active", not "archived".
status = "archived";
status = "active"; // allowedThe let variable can still be assigned. Its type simply permits one value. If the binding should not be reassigned, use const as well:
const status = "active" as const;Remember that const and as const answer different questions. const restricts reassignment of a variable binding. as const narrows the type of a literal expression and adds readonly properties or tuple elements.
It does not replace validation
This assertion preserves the value that you wrote. It does not check that the value follows a required shape.
const route = {
path: "/users",
methd: "GET",
} as const;TypeScript accepts the misspelled methd property because no expected route type was provided. If you need shape checking while preserving literal types, combine as const with satisfies:
type Route = {
path: string;
method: "GET" | "POST";
};
const route = {
path: "/users",
method: "GET",
} as const satisfies Route;Now an incorrect property name or invalid method produces a compile-time error, while route.method remains the literal type "GET".
A practical decision rule
Use as const when a literal value is intentionally fixed for type-level work, such as deriving a union or creating a readonly tuple. Do not use it as a substitute for runtime validation, deep immutability, or a general type conversion.
Exercise
What happens when server.port is changed through the original server variable after { server } as const is created?
Show answer
The change is visible through the const-asserted object because as const makes the property reference readonly, but it does not freeze the object referenced by that property.