Sub topic 3
`as const satisfies`: Preserve Literals While Checking Types
This chapter assumes you know TypeScript unions, Record, object literals, as const, and type inference.
as const preserves exact literal values and makes object properties and arrays readonly. satisfies checks that the resulting value matches a required type without replacing its inferred type.
type Page = "home" | "users";
type Route = `/${string}`;
const routes = {
home: "/",
users: "/users",
} as const satisfies Record<Page, Route>;
type RouteValue = (typeof routes)[keyof typeof routes];
// "/" | "/users"
console.log(routes.users); // "/users"The object must contain every Page key, and every value must start with /. At the same time, routes.users remains the literal type "/users", rather than widening to string.
Choosing the right tool
Use a type annotation when you want the variable to have one declared type, even if that loses property-specific detail.
const config: Record<Page, Route> = {
home: "/",
users: "/users",
};Use as when you have stronger type knowledge than TypeScript can infer. An assertion can hide mistakes, so it does not validate the value in the same protective way.
const config = { home: "/", users: "/users" } as Record<Page, Route>;Use satisfies when you want validation while retaining the expression’s precise inferred type. Combine it with as const when exact values or readonly data matter.
| Tool | Checks compatibility | Preserves specific inferred properties |
|---|---|---|
| Annotation | Yes | Usually no |
| `as` assertion | No reliable validation | Often |
| `satisfies` | Yes | Yes |
Limitations and pitfalls
satisfies only performs compile-time checking. It does not validate JSON, user input, or API responses at runtime.
Also remember that satisfies checks assignability, not business rules. A route can have the correct string shape but still point to a nonexistent page.
Exercise
Which expression preserves the literal type of "/users" while checking that all keys are valid routes?
Show answer
{ users: "/users" } as const satisfies Record<"users", \/${string}\>.