TypeScript · Advanced · 18 min
Literal configurations
Check configuration shapes while preserving useful literal types.
Validate with satisfies
The "satisfies" operator checks that an expression fits a contract while retaining its useful inferred type. Here the literal mode stays specific. It does not validate data arriving at runtime.
type Mode = "focus" | "review";
const settings = { mode: "focus", minutes: 20 } satisfies { mode: Mode; minutes: number };
const selected: "focus" = settings.mode;
console.log(selected);Output
focus
Derive choices from constant data
A const assertion preserves literal values and makes literal object properties and array entries readonly in the type system. It does not freeze objects at runtime. Derive a union from the tuple so allowed choices stay aligned with the data.
const topics = ["Travel", "Music"] as const;
type Topic = typeof topics[number];
const selected: Topic = "Music";
console.log(selected);Output
Music
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Correct a configuration
Replace the invalid mode with "focus". Keep "satisfies" and the specific type of "selected".
The configuration must match the allowed mode union.
Show solution
type Mode = "focus" | "review";
const settings = { mode: "focus", minutes: 20 } satisfies { mode: Mode; minutes: number };
const selected: "focus" = settings.mode;
console.log(selected);The corrected literal meets the contract and remains specific enough for the selected variable. The check emits no runtime validation.
Put it into practice
- Predict the output and explain which relationships the types enforce.
- Fix the task while preserving its type contract, then check both practice questions.