Sub topic 8
`as const satisfies`: Literal Types That Are Still Validated
Assume you know TypeScript unions, literal types, object literals, arrays, tuples, Record, and type inference. This chapter focuses on one pattern:
const value = {
// ...
} as const satisfies SomeType;This combines two operations:
as constpreserves exact literal values and makes the result deeplyreadonly.satisfieschecks that the value matches a required type without replacing the inferred type.
The order matters. TypeScript first applies as const, then checks the result with satisfies.
Why combine `as const` and `satisfies`?
Consider a route configuration:
const routes = {
home: "/",
user: "/users/:id",
} as const satisfies Record<string, `/${string}`>;The Record<string, \/${string}\> type requires every property value to be a string beginning with /.
At the same time, as const preserves the exact values. TypeScript infers the type as:
{
readonly home: "/";
readonly user: "/users/:id";
}Without as const, the values would usually widen to string:
const routes = {
home: "/",
user: "/users/:id",
} satisfies Record<string, `/${string}`>;
// The values are treated as strings rather than exact literals.Both versions pass validation, but only the first version keeps the exact route literals.
The exact values are useful when another type is derived from the object:
const routes = {
home: "/",
user: "/users/:id",
} as const satisfies Record<string, `/${string}`>;
type Route = (typeof routes)[keyof typeof routes];
// "/" | "/users/:id"
function navigateTo(route: Route) {
console.log(`Navigating to ${route}`);
}
navigateTo(routes.home);
navigateTo(routes.user);Route becomes a union of the object’s values. This gives you a checked list of allowed routes without writing the union separately.
`as const` preserves values and adds `readonly`
as const performs a const assertion. For primitive values, it keeps the narrowest literal type:
const settings = {
mode: "dark",
retries: 3,
enabled: true,
} as const;The inferred type is equivalent to:
{
readonly mode: "dark";
readonly retries: 3;
readonly enabled: true;
}The properties cannot be reassigned:
// Error: Cannot assign to 'mode' because it is a read-only property.
// settings.mode = "light";Nested objects and arrays also become readonly:
const palette = {
primary: [255, 0, 0],
secondary: [0, 0, 255],
} as const;
// primary has type readonly [255, 0, 0]The array is inferred as a readonly tuple. A tuple is an array whose length and position types are known.
Validate object keys and values
The satisfies part still performs normal compatibility checks. This makes the pattern useful for configuration objects.
type Theme = "light" | "dark";
type ThemeConfig = {
label: string;
foreground: `#${string}`;
};
const themes = {
light: {
label: "Light theme",
foreground: "#111111",
},
dark: {
label: "Dark theme",
foreground: "#eeeeee",
},
} as const satisfies Record<Theme, ThemeConfig>;The Record<Theme, ThemeConfig> type requires both light and dark keys. Each value must contain a string label and a foreground string beginning with #.
A misspelled key or missing key is rejected:
type Theme = "light" | "dark";
const themes = {
light: {
label: "Light theme",
foreground: "#111111",
},
drak: {
label: "Dark theme",
foreground: "#eeeeee",
},
} as const satisfies Record<Theme, { label: string; foreground: `#${string}` }>;
// Error: 'dark' is missing.
// Error: 'drak' is not a known key.The object still retains its individual literal values when it passes validation. For example, themes.light.label is known as "Light theme", not merely string.
The readonly array caveat
Because as const creates readonly arrays and tuples, the type on the right side of satisfies must accept readonly values.
This fails:
// Error: a readonly tuple cannot be assigned to mutable string[].
// const bad = ["a", "b"] as const satisfies string[];A mutable string[] allows changes such as push. A readonly tuple does not allow those changes, so the types are incompatible.
Use readonly string[] instead:
const labels = ["first", "second"] as const satisfies readonly string[];
console.log(labels[0]); // "first"
// labels.push("third"); // Error: push does not exist on a readonly tupleThe inferred type is:
readonly ["first", "second"]For readonly nested arrays, describe the expected structure as readonly too:
type RGB = readonly [number, number, number];
const colors = {
red: [255, 0, 0],
blue: [0, 0, 255],
} as const satisfies Record<string, RGB>;Here, each value is a readonly tuple, and RGB explicitly permits that.
A practical decision
Use a type annotation when you want the variable to have exactly the declared type, even if that widens specific values:
const config: Record<string, string> = {
environment: "production",
};Use as const when preserving literals and readonly data is the main goal.
Use satisfies when validation is the main goal but you also want TypeScript to retain the expression’s inferred details.
Use as const satisfies when you need both:
- exact literal keys and values,
- readonly data,
- validation of keys and values,
- useful types derived with
typeofandkeyof.
Exercise
Why does const values = ["a", "b"] as const satisfies string[] fail, and what change fixes it?
Show answer
as const creates a readonly tuple, but string[] is mutable. Replace string[] with readonly string[], producing const values = ["a", "b"] as const satisfies readonly string[].