Sub topic 4
Keeping Literal Types with `as const`
This chapter assumes you know basic TypeScript types, function annotations, object literals, and switch statements. You will learn how as const prevents TypeScript from widening specific values into general types.
Why literal types widen
A literal type describes one exact value. For example, "dark" is a string literal type, while string includes every string.
TypeScript often widens a value when it infers a type. Widening means replacing a specific literal type with its broader base type:
"dark"can widen tostring.42can widen tonumber.truecan widen toboolean.
This behavior is useful for mutable values. If a variable can change later, TypeScript usually gives it a type that can hold different values.
let direction = "up";
// direction has type string
direction = "down"; // Allowed
direction = "sideways"; // Also allowedThe compiler infers direction as string because let variables are mutable.
A const variable cannot be reassigned, so TypeScript preserves a primitive literal more often:
const direction = "up";
// direction has type "up"However, const only prevents reassignment of the variable itself. It does not preserve literal types inside an object or array.
const settings = {
mode: "dark",
};
settings.mode = "light"; // Allowed
// settings.mode has type stringThe object is still mutable, so its mode property widens to string.
What `as const` does
as const is a const assertion. It tells TypeScript to preserve literal values as narrow types and make object properties and arrays readonly.
const settings = {
mode: "dark",
retries: 3,
enabled: true,
} as const;
// Types:
// settings.mode: "dark"
// settings.retries: 3
// settings.enabled: trueThe object is deeply readonly for its literal properties:
settings.mode = "light";
// Error: Cannot assign to 'mode' because it is a read-only propertyFor arrays, as const creates a readonly tuple. A tuple is an array type with a fixed number of elements and a known type at each position.
const coordinates = [10, 20] as const;
// type: readonly [10, 20]
const x = coordinates[0]; // type: 10
const y = coordinates[1]; // type: 20Without as const, the same array is usually inferred as number[], which allows any number of elements.
Deriving useful types from constants
A common pattern uses an as const object as a runtime value and derives a union type from it.
const Status = {
Active: "active",
Inactive: "inactive",
Pending: "pending",
} as const;
type Status = (typeof Status)[keyof typeof Status];
function label(status: Status): string {
switch (status) {
case Status.Active:
return "Currently active";
case Status.Inactive:
return "Currently inactive";
case Status.Pending:
return "Waiting for approval";
}
}
console.log(label(Status.Active));
console.log(label("pending"));The expression typeof Status obtains the type of the object. keyof typeof Status produces the key union "Active" | "Inactive" | "Pending". Indexing that object type with those keys produces the value union "active" | "inactive" | "pending".
This pattern gives you named runtime values without requiring an enum.
When `as const` is not appropriate
as const makes values readonly. Do not use it when the code must mutate the object or array.
const tasks = ["write", "test"] as const;
// tasks.push("deploy");
// Error: Property 'push' does not exist on type 'readonly ["write", "test"]'.You can keep a mutable collection and annotate its allowed values separately:
type Task = "write" | "test" | "deploy";
const tasks: Task[] = ["write", "test"];
tasks.push("deploy"); // AllowedAlso remember that as const does not validate external data. A value read from JSON or a network request still needs runtime validation before you trust it as a fixed set of values. A type assertion can affect compiler checking, but it cannot change the value at runtime.
Exercise
What are the inferred types of let a = "up", const b = "up", and const c = { direction: "up" } as const?
Show answer
a is string, b is the literal type "up", and c.direction is the readonly literal type "up".