Skip to the page

TypeScript in real projects · mid

satisfies, as const, enums and assertions

as const keeps literal types and makes values readonly, satisfies checks a value against a type without widening it, and as assertions skip checks, so they are the last resort.

The notes

What you must know

  • const x = { mode: 'dark' } as const gives mode: 'dark' (readonly), not string.
  • const routes = {...} satisfies Record<string, Route> errors on bad entries but keeps the exact keys for autocomplete.
  • x as User tells the compiler to trust you: no check, a common source of runtime bugs. Prefer narrowing or validation.
  • Enums emit runtime code and numeric enums accept any number; a union of string literals (or an as const object) is usually better. erasableSyntaxOnly (TS 5.8) bans enums for type-stripping runtimes like Node.

The red pen

Common traps: mistake → fix

  • Mistake: "satisfies is the same as a type annotation"

    Fix: an annotation widens the value to the type; satisfies keeps the narrower inferred type.

  • Mistake: "as converts the value"

    Fix: it changes nothing at runtime.

Easy to hard

Questions interviewers ask, easy to hard

  1. When do you use satisfies over an annotation?
  2. Why do many teams avoid enums?
  3. When is an as assertion acceptable?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview