Check an object against a type with satisfies
Validate a config object against an interface while keeping its precise literal types.
- TypeScript
- GreenSnippets
- MIT
- Executed with Node.js 22 (type stripping)
type Route = { path: string; auth?: boolean };
const routes = {
home: { path: "/" },
account: { path: "/account", auth: true },
} satisfies Record<string, Route>;
type RouteName = keyof typeof routes; // "home" | "account"
function pathFor(name: RouteName): string {
return routes[name].path;
}Example / expected output
pathFor("account"); // "/account"
pathFor("missing"); // compile errorHow it works
A type annotation (const x: Config = ...) widens values to the declared type and forgets literals. satisfies checks the shape without widening, so you get both validation and autocompletion on the exact keys and values you wrote. Combine with as const for readonly tuples.
Related snippets
- Python Tested
Read typed settings from environment variables
Load configuration from the environment with defaults, type conversion and clear errors for missing values.
- TypeScript Tested
Non-empty array type
Encode "at least one element" in the type so head, max and similar functions never see an empty array.
- TypeScript Tested
Branded types for IDs
Prevent mixing up UserId and OrderId strings by giving each a unique compile-time brand.
- TypeScript Tested
Assert a value is not null or undefined
An assertion function that narrows away null and undefined and throws a clear error otherwise.

