Advanced TypeScript
`satisfies`, `as const` & Literal Types
Literal types preserve exact values.
as const freezes inference to readonly literal shapes. satisfies checks a value against a target type without widening away its precise inferred type.- Literal types represent exact values
as constpreserves literals deeplysatisfieschecks without replacing inference- This is ideal for configuration objects
as Typeis not the same
type Route = { path: string; auth: boolean }
const routes = {
home: { path: "/", auth: false },
admin: { path: "/admin", auth: true },
} satisfies Record<string, Route>
type RouteName = keyof typeof routes
// "home" | "admin"const statuses = ["idle", "loading", "success", "error"] as const
type Status = typeof statuses[number]
// "idle" | "loading" | "success" | "error"| Tool | What it does | Use for |
|---|---|---|
| literal type | Exact value type | Variants and constants |
| as const | Readonly literal inference | Arrays/config values |
| satisfies | Check shape, keep inference | Config maps |
| as Type | Force compiler belief | Last resort / interop |