TYPESCRIPT / CHAPTER 06

Literal inference and checked configuration

Intermediate

Retain useful specific types without pretending assertions validate data.

By the end of this chapter

  • Recognize literal widening
  • Use const assertions selectively
  • Check configuration with satisfies

The idea

A mutable property often needs a broader type than its initial value. Even when an object variable is declared const, its ordinary properties can change, so a string property may be inferred as string rather than one literal. A const assertion preserves literal information and marks object properties or tuple elements readonly in the inferred type; it does not freeze the runtime object.

The satisfies operator checks compatibility while retaining the expression’s resulting inferred type instead of replacing it with a broad annotation. This is useful for configuration dictionaries whose individual entries have more specific shapes than the overall allowed value type. These examples assume TypeScript 4.9 or later and strict checking. Treat compiler checks as design feedback, while continuing to validate external data at runtime.

TypeScript example
const palette = {
  label: "violet",
  rgb: [90, 40, 150]
} satisfies Record<string, string | number[]>;
console.log(palette.label.toUpperCase());
console.log(palette.rgb.length);
Result
VIOLET
3

Each property retains a useful specific type while the object is checked against the configuration contract.

Try it yourself

Define a checked configuration with named string and numeric-array entries while keeping operations specific to each entry.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

Which declaration retains mode as the literal type "read"?