as const and satisfies

Topic 1 of 4 in Modelling data7 min readIntermediateFree course

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.

Plain, as const and satisfies, compared with type Theme = { mode: “light” | “dark”; sizes: number[] }. A plain const a = { mode: “dark”, sizes: [12, 16] } is inferred as mode: string and sizes: number[] — it loses the exact values. const b = { … } as const is inferred as readonly mode: “dark” and readonly sizes: readonly [12, 16] — exact and read-only, but unchecked. const c = { … } satisfies Theme gives mode: “dark” and sizes: number[], and is checked against Theme — checked and keeps “dark”. satisfies catches typos (“drak”) while keeping the precise type; as const freezes values into literal types.

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.
TypeScript
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.

TypeScript
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:

TypeScript
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 forgotten

satisfies checks the value against the type without changing what TypeScript infers:

TypeScript
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:

TypeScript
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.

TypeScript
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 string

The 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 (+ satisfies if 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”; satisfies says “check this against Type, but remember exactly what it is”; as const says “these values will never change”.

Exercise

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.

TypeScript
type Theme = {
  mode: "light" | "dark";
  colors: Record<string, `#${string}`>;
};

// 1. ROUTES, RouteName, link()

// 2. theme with satisfies

// 3. SIZES and Size

Summary

  • as const makes TypeScript infer the narrowest types: literal values, readonly properties and readonly tuples.
  • satisfies Type checks 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 const data with typeof and keyof, so values and types can’t drift apart.
  • Combine them — { … } as const satisfies Type — for checked, read-only, exact configuration.