Careeroza
The satisfies Operator
Check Without Widening
value satisfies Type (TypeScript 4.9+) checks that a value matches a type without changing the inferred type. With an annotation (const x: Type = ...) you lose the specific details; with satisfies you keep them and still get the check.
Example
With the annotation, routesA.home is string | RouteObj, so calling .toUpperCase() needs a check. With satisfies, TypeScript remembers that home is a string and product is an object.
type RouteObj = { path: string; auth: boolean };
type Routes = Record<string, string | RouteObj>;
const routesA: Routes = { home: "/", product: { path: "/product/:id", auth: false } };
const routesB = {
home: "/",
product: { path: "/product/:id", auth: false },
orders: { path: "/orders", auth: true },
} satisfies Routes;
console.log(typeof routesA.home);
console.log(routesB.home.toUpperCase());
console.log(routesB.orders.auth);Output
string / true
satisfies Still Catches Mistakes
Example
A wrong value type is reported (TS2322), exactly like an annotation would.
type Routes = Record<string, string | { path: string; auth: boolean }>;
const routes = {
home: "/",
cart: { path: "/cart", auth: "yes" },
} satisfies Routes;Compiler output (tsc)
Line 4: error TS2322: Type 'string' is not assignable to type 'boolean'.