Sub topic 3
Readonly objects with literal properties
Assume you know basic TypeScript types, object types, union types, and the difference between let and const. This chapter focuses on how as const changes the inferred type of an object literal.
Why object properties widen
TypeScript often widens a literal value when it appears in a mutable position. For example:
let mode = "dark";
// string
const name = "Ada";
// "Ada"
const config = { mode: "dark", retries: 3 };
// { mode: string; retries: number }The variable name cannot be reassigned, so TypeScript can safely infer the exact literal type "Ada". The object assigned to config is different. Even though config itself is declared with const, its properties can still change:
config.mode = "light";
config.retries = 4;Because these assignments are allowed, TypeScript infers mode as string and retries as number. The property types must be broad enough to describe future values.
A const variable prevents reassignment of the variable binding. It does not make the object immutable.
What `as const` does
A const assertion uses the syntax as const after a literal expression:
const config = { mode: "dark", retries: 3 } as const;Now TypeScript infers:
{
readonly mode: "dark";
readonly retries: 3;
}This has two effects:
- Literal values remain literal types.
- Properties become
readonly, so TypeScript rejects assignments to them.
const config = { mode: "dark", retries: 3 } as const;
const mode: "dark" = config.mode;
const retries: 3 = config.retries;
// config.mode = "light"; // Error
// config.retries = 4; // ErrorThe exact type is useful when a value represents a fixed configuration, a command, or a set of known labels. The readonly modifier protects that type assumption inside the TypeScript program.
| Declaration | Inferred property type |
|---|---|
| `{ mode: "dark" }` | `{ mode: string }` |
| `{ mode: "dark" } as const` | `{ readonly mode: "dark" }` |
A guided example
Suppose an application accepts only two display modes:
type Mode = "light" | "dark";
const defaults = {
mode: "dark",
compact: false,
} as const;The inferred type of defaults is:
{
readonly mode: "dark";
readonly compact: false;
}This type is narrower than the general application settings type:
type Settings = {
mode: Mode;
compact: boolean;
};The narrow value can be used wherever the wider type is expected:
function printSettings(settings: Settings): void {
console.log(`${settings.mode}, compact: ${settings.compact}`);
}
printSettings(defaults);A value with the literal type "dark" is assignable to Mode because "dark" is one member of the union. A value with the literal type false is assignable to boolean.
However, the reverse is not always safe:
let settings: Settings = {
mode: "light",
compact: true,
};
// const onlyDark: "dark" = settings.mode; // ErrorThe settings.mode property might contain either "light" or "dark", so it cannot be assigned to a variable that accepts only "dark".
Readonly applies to the typed value
With as const, TypeScript prevents direct property assignment:
const command = {
type: "refresh",
options: {
force: true,
},
} as const;
// command.type = "delete"; // Error
// command.options.force = false; // ErrorThe nested object literal is also inferred with readonly properties. This lets TypeScript preserve the fixed shape of a literal configuration.
But as const does not freeze an object at runtime. JavaScript still stores an ordinary object:
const settings = { mode: "dark" } as const;
(settings as { mode: string }).mode = "light";
console.log(settings.mode);
// "light" at runtimeThe type assertion in this example deliberately hides the readonly property from the compiler. The assignment then runs normally. This demonstrates that as const is a compile-time feature, not a runtime security mechanism.
Choosing `as const` for an object
Use as const when all of these are true:
- The object represents a fixed value or declaration.
- Exact string, number, or boolean types are useful.
- Callers should not modify the value through its inferred type.
- You want TypeScript to infer the type directly from the value.
For mutable application state, as const is usually the wrong choice. State such as a user preference or an editing form must often change, so a regular object type is clearer:
type UserSettings = {
mode: Mode;
compact: boolean;
};
let current: UserSettings = {
mode: "light",
compact: false,
};
current.mode = "dark";
current.compact = true;A common mistake is to add as const merely because a variable is declared with const. The two features solve different problems:
const mutableObject = { count: 0 };
mutableObject.count = 1; // Allowed
const readonlyObject = { count: 0 } as const;
// readonlyObject.count = 1; // ErrorThe first binding cannot point to a different object, but the object itself can change. The second object has a readonly count property in its TypeScript type.
Exercise
Exercise
What is the inferred type of const response = { status: "ok", code: 200 } as const, and which assignments are rejected: response.status = "error" or const s: string = response.status?
Show answer
The inferred type is { readonly status: "ok"; readonly code: 200 }. The assignment to response.status is rejected because the property is readonly. The assignment to s is allowed because the literal type "ok" is assignable to string.