The 'satisfies' operator is one of the most useful additions to TypeScript in recent versions. It lets you validate that an expression matches a type while preserving the most specific type possible.
The Problem
When you annotate a variable with a type, TypeScript widens the type. This means you lose specific literal types.
typescript
1// Type annotation widens the type2const config: Config = {3 port: 3000,4 host: "localhost"5};6 7// config.port is number | string, not 30008// You lose the literal type!The Solution: satisfies
With 'satisfies', you validate the type while keeping inference:
typescript
1const config = {2 port: 3000,3 host: "localhost"4} satisfies Config;5 6// config.port is 3000 (literal type preserved!)7// config.host is "localhost" (literal type preserved!)8 9// Still type-safe - this would error:10const badConfig = {11 port: "invalid" // Error: string not assignable to number12} satisfies Config;Real-World Example
This is especially useful for configuration objects and constants:
typescript
1const routes = {2 home: "/",3 about: "/about",4 user: (id: string) => `/user/${id}`5} satisfies Record<string, string | ((...args: any[]) => string)>;6 7// routes.home is "/" not string8// routes.user is (id: string) => string not ((...args: any[]) => string)9 10// Autocomplete still works perfectly!