as const and satisfies
as const tells TypeScript to infer the narrowest literal types and mark object properties/arrays readonly in the type system. The satisfies operator checks that an expression conforms to a target type while retaining the expression's more specific inferred type. This is particularly useful for configuration objects where you want validation without losing literal information.
Neither construct validates data at runtime. They also do not make objects deeply immutable at runtime. Use runtime schema validation for data from APIs, storage, environment variables, or user input.
Validate a config while preserving literals
type RouteName = "home" | "docs";
const routes = {
home: "/",
docs: "/documentation"
} as const satisfies Record<RouteName, string>;
const docsPath = routes.docs; // inferred as "/documentation"💡
satisfies checks that all required route keys exist and their values are strings. as const keeps the exact path literals, so routes.docs is not widened to a general string.Key Takeaways
satisfieschecks compatibility without replacing the inferred expression type.as constpreserves literals and readonly typing.- Neither feature performs runtime data validation.
Knowledge Check
How does `satisfies` differ from a direct type annotation in many cases?