What is the satisfies operator in TypeScript

· Category: TypeScript

Short answer

The satisfies operator checks that a value conforms to a type while preserving the value's specific literal inference. It bridges the gap between type annotations and inferred types. For broader type system concepts, see python type hints. For writing robust code, see python error handling try except.

Steps

  1. Define a configuration object: const config = { host: "localhost", port: 3000 } satisfies Record<string, string | number>
  2. TypeScript validates the shape without widening the inferred literals
  3. Access properties with full autocomplete for literal values
  4. Use it for API responses and config objects
  5. Combine with as const for stricter immutability

Tips

  • satisfies is useful for object literals where you want inference plus validation
  • Prefer satisfies over type annotations when you need narrow literal types
  • For testing type behavior, see python testing with pytest