Skip to content

Configuration & satisfies

The Course
•
20 min study

An annotation declares the type a variable should be viewed through. satisfies checks that an expression is assignable to a type while retaining its resulting inferred type rather than simply treating the variable as the broad target annotation. It does not validate unknown runtime data.

For a small configuration, this can catch a misspelled field or an invalid value while preserving a specific mode. Read the operator as “this expression must meet this contract.” Context still influences inference; do not assume satisfies freezes every literal or means exactly the same inference in every expression.

Use ordinary annotations when you want an intentionally broad view and satisfies for a literal configuration whose precise shape remains useful. Neither makes an object immutable. as const, introduced with tuples tomorrow, is a separate literal-preservation tool and also does not freeze runtime data.

Keep configuration validation and external input validation separate. A checked literal in your source is different from a JSON value arriving from a server. This lesson is about authoring a known configuration, not trusting an arbitrary one.

Predict, run, and explain

Write down what the program prints before you run it. Use Check types first; the program runs only when the real compiler accepts it.

Predict, then runTypeScript
type Options = {
  mode: "safe" | "fast";

  limit: number;
};

const options = { mode: "safe", limit: 2 } satisfies Options;

function requireSafe(mode: "safe"): string {
  return mode;
}

console.log(requireSafe(options.mode));
console.log(options.limit);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
safe
2

Read the compiler error

The configuration is a known literal, but the limit still violates its contract.

Find and fix the type errorTypeScript
type Options = {
  mode: "safe" | "fast";

  limit: number;
};

const options = { mode: "safe", limit: "2" } satisfies Options;

console.log(options);

Enable JavaScript to edit and type-check this example.

Check your diagnosis

Keep limit numeric. satisfies checks compatibility; it is not a conversion operation.

Keep the inferred shape after checking the contract

An annotation and satisfies can both reject an invalid configuration, but they present the value differently to later code. A broad annotation intentionally exposes a broad view. Satisfies checks an expression while retaining its resulting inferred shape and contextual literal choices.

Neither operation freezes the value or validates external JSON. Use these tools for known source literals, then keep runtime decoders for data arriving after compilation. Read the inferred field types as well as whether the first assignment passed.

satisfies keeps the literal modeTypeScript
type Options = {
  mode: "safe" | "fast";

  limit: number;
};

const options = { mode: "safe", limit: 2 } satisfies Options;

function safe(mode: "safe"): string {
  return mode;
}

console.log(safe(options.mode));
options.limit = 3;
console.log(options.limit);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
safe
3

The mode retains the specific safe choice, while limit remains numeric and writable. Satisfies is not shorthand for making every field a readonly literal.

Now the same configuration with an ordinary annotation. It is just as valid an Options, and that is the problem: later code sees only what the annotation says.

Find and fix the type error: annotation instead of satisfiesTypeScript
type Options = {
  mode: "safe" | "fast";

  limit: number;
};

const options: Options = { mode: "safe", limit: 2 };

function requireSafe(mode: "safe"): string {
  return mode;
}

console.log(requireSafe(options.mode));

Enable JavaScript to edit and type-check this example.

Check your diagnosis

The key line is TS2345: Argument of type '"safe" | "fast"' is not assignable to parameter of type '"safe"'. The first type in the message is what the checker knows about options.mode: the annotation widened it to the whole union, so the checker has forgotten that you wrote "safe". Replace : Options with satisfies Options after the object literal. The object is still checked against Options, but options.mode keeps the type "safe" and the call compiles. That retained detail is the whole reason to choose satisfies.

Try the rule on new inputs

Try a misspelled key, a missing key and a wrong value type separately. Then compare satisfies with an intentionally broad annotation and describe which later calls can use the retained literal information.

Name a value's type and its keys: typeof and keyof

The practice below, and today's checkpoint, derive types from a configuration object, so they need two type operators. Tomorrow's keyof lesson covers them in depth; here is exactly what you need today.

  • In a type position, typeof sizes means "the type of the value sizes". This is not the runtime typeof from the beginner course, which returns a string such as "number". This one exists only for the checker.
  • keyof SomeType is the union of that type's property names. So keyof typeof sizes reads "the keys of the type of sizes".
Derive types from a valueTypeScript
const sizes = { small: 10, large: 20 };

type Sizes = typeof sizes;
type SizeName = keyof typeof sizes;

const shape: Sizes = { small: 1, large: 2 };
const name: SizeName = "large";

console.log(name, sizes[name], shape.small);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
large 20 1

SizeName is "small" | "large", so const name: SizeName = "medium" fails with TS2322 Type '"medium"' is not assignable to type '"small" | "large"', and sizes[name] is known to be a number. This only works if the object kept its exact keys. Annotate it as Record<string, number> and keyof typeof sizes is just string, so "medium" compiles. That is where satisfies comes in.

Your turn

Exercise

Configuration & satisfies: practice

A message table must have string values, but its keys are whatever messages the app defines. Create messages with loading → "Loading" and ready → "Ready", checked against Record<string, string>. Then write message(key: keyof typeof messages): string returning the uppercased message. Print LOADING and READY, and make sure message("done") is a compile error. Try it with an annotation first and read what goes wrong.

Your workspaceTypeScript
// Define messages, then a message function whose key type comes from the table.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

With const messages: Record<string, string>, keyof typeof messages is just string, so "done" is accepted and every lookup may be undefined. You need the check without losing the two literal keys.

Hint 2: a first step

Put satisfies Record<string, string> after the closing brace of the object literal, then hover or reason about what keyof typeof messages is now.

Show one solution
One possible solutionTypeScript
const messages = {
  loading: "Loading",
  ready: "Ready"
} satisfies Record<string, string>;

function message(key: keyof typeof messages): string {
  return messages[key].toUpperCase();
}

console.log(message("loading"));
console.log(message("ready"));

Enable JavaScript to edit and type-check this example.

Text
LOADING
READY

satisfies checks every value is a string (ready: 2 would fail with TS2322) while typeof messages keeps the exact keys, so keyof typeof messages is "loading" | "ready". That gives two compile-time guarantees: message("done") fails with TS2345, and messages[key] is a definite string. With the annotation instead, the key type was string, "done" compiled, and messages[key].toUpperCase() failed with TS2532 (possibly undefined). Nothing here checks data at runtime; a table loaded from JSON would still need a decoder.

Change it and test it

Remove ready and inspect the real diagnostic, then restore it. Try a numeric label. Explain whether a clean satisfies check would inspect values loaded from JSON at runtime.

Was this article helpful?

Your response is saved on this device.