as const and satisfies
Overview
TypeScript usually infers useful, general types: "dark" becomes string, [12, 16] becomes number[]. That’s what you want for variables that change. But for configuration and constant data you often want the opposite: exact values, read-only, and checked against a type. Two operators give you precise control: as const and satisfies.
as const
Add as const after an object or array literal, and TypeScript infers:
- literal types instead of
string/number; - readonly properties;
- readonly tuples instead of arrays.
const config = {
env: "production",
retries: 3,
hosts: ["a.example.com", "b.example.com"],
} as const;
// {
// readonly env: "production";
// readonly retries: 3;
// readonly hosts: readonly ["a.example.com", "b.example.com"];
// }
config.retries = 5;
// error TS2540: Cannot assign to 'retries' because it is a read-only property.It works on single values too: let mode = "dark" as const gives mode the type "dark".
Deriving types from constant data
The big win: you can write the data once and derive types from it, instead of listing the same values twice.
const SIZES = ["sm", "md", "lg"] as const;
type Size = (typeof SIZES)[number]; // "sm" | "md" | "lg"
const ROUTES = { home: "/", cart: "/cart" } as const;
type RouteName = keyof typeof ROUTES; // "home" | "cart"
type RoutePath = (typeof ROUTES)[RouteName]; // "/" | "/cart"typeof turns a value into its type, keyof takes the keys of an object type, and [number] gets an array’s element type (all covered properly in the Type operators chapter). Add "xl" to SIZES, and the Size type updates itself; you also have a real array to loop over at run time — something a type alone can’t give you.
satisfies
An annotation checks a value, but it also replaces the inferred type:
type Theme = { mode: "light" | "dark"; colors: Record<string, string> };
const theme: Theme = {
mode: "dark",
colors: { brand: "#2d5bd7" },
};
theme.colors.brand; // string — fine
theme.colors.brnad; // also "fine": colors is Record<string, string>, any key allowed
theme.mode; // "light" | "dark" — the exact value is forgottensatisfies checks the value against the type without changing what TypeScript infers:
const theme = {
mode: "dark",
colors: { brand: "#2d5bd7" },
} satisfies Theme;
theme.mode; // "dark"
theme.colors.brand; // string
theme.colors.brnad;
// error TS2551: Property 'brnad' does not exist on type '{ brand: string; }'. Did you mean 'brand'?And mistakes in the value are still caught where you wrote them:
const theme = { mode: "drak", colors: {} } satisfies Theme;
// error TS2322: Type '"drak"' is not assignable to type '"dark" | "light"'.In short:
| You write | TypeScript checks against Theme? | Type of the variable |
|---|---|---|
const t = { … } |
no | inferred (general) |
const t: Theme = { … } |
yes | Theme |
const t = { … } satisfies Theme |
yes | inferred (precise) |
const t = { … } as const |
no | inferred (exact, readonly) |
Combining them
For configuration objects, the combination is often perfect: checked against a type, exact and read-only.
type Route = { path: string; auth: boolean };
const ROUTES = {
home: { path: "/", auth: false },
account: { path: "/account", auth: true },
} as const satisfies Record<string, Route>;
type RouteName = keyof typeof ROUTES; // "home" | "account" — not just stringThe order matters: as const first, then satisfies.
Don’t confuse as const with as
as const is safe: it only makes types more precise, based on the literal you wrote. A plain type assertion like value as User is different — it tells TypeScript to trust you and can hide real errors. If you find yourself writing as SomeType to make a literal fit, reach for satisfies instead.
When to use what
- Constants, lookup tables, route maps, option lists:
as const(+satisfiesif there’s a type to conform to). - Configuration that must match a type, but where you want precise autocomplete:
satisfies. - Variables that change, function parameters and return types: ordinary annotations.
Remember: annotations say “treat this as Type”;
satisfiessays “check this against Type, but remember exactly what it is”;as constsays “these values will never change”.
Typed configuration
(1) Make ROUTES an as const object mapping route names (home, products, cart) to paths, and derive type RouteName = keyof typeof ROUTES. Write link(name: RouteName): string returning <a href="/cart">cart</a>. (2) Declare const theme = { … } satisfies Theme with mode: "dark" and a colors record of named hex strings, so that theme.mode keeps the literal type "dark" and theme.colors.brand is a known key. (3) Make const SIZES = ["sm", "md", "lg"] as const and derive type Size = (typeof SIZES)[number]; loop over SIZES printing each. Show with // @ts-expect-error that link("checkout") is rejected and that a typo mode: "drak" would be caught by satisfies.
type Theme = {
mode: "light" | "dark";
colors: Record<string, `#${string}`>;
};
// 1. ROUTES, RouteName, link()
// 2. theme with satisfies
// 3. SIZES and Sizetype Theme = {
mode: "light" | "dark";
colors: Record<string, `#${string}`>;
};
// 1. ROUTES, RouteName, link()
const ROUTES = {
home: "/",
products: "/products",
cart: "/cart",
} as const;
type RouteName = keyof typeof ROUTES; // "home" | "products" | "cart"
function link(name: RouteName): string {
return `<a href="${ROUTES[name]}">${name}</a>`;
}
// 2. theme with satisfies
const theme = {
mode: "dark",
colors: { brand: "#2d5bd7", text: "#1b1d21" },
} satisfies Theme;
const mode: "dark" = theme.mode; // still the literal "dark"
console.log(link("cart"), mode, theme.colors.brand);
// 3. SIZES and Size
const SIZES = ["sm", "md", "lg"] as const;
type Size = (typeof SIZES)[number]; // "sm" | "md" | "lg"
for (const size of SIZES) {
const s: Size = size;
console.log(s);
}
// @ts-expect-error -- not a route name
link("checkout");
const broken = {
// @ts-expect-error -- satisfies catches the typo
mode: "drak",
colors: {},
} satisfies Theme;
console.log(broken.colors);Summary
as constmakes TypeScript infer the narrowest types: literal values, readonly properties and readonly tuples.satisfies Typechecks a value against a type but keeps the value’s own, more precise inferred type.- An annotation (
const x: Type = …) checks too, but replaces the inferred type with the annotation. - Derive types from
as constdata withtypeofandkeyof, so values and types can’t drift apart. - Combine them —
{ … } as const satisfies Type— for checked, read-only, exact configuration.