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
- Define a configuration object:
const config = { host: "localhost", port: 3000 } satisfies Record<string, string | number> - TypeScript validates the shape without widening the inferred literals
- Access properties with full autocomplete for literal values
- Use it for API responses and config objects
- Combine with
as constfor stricter immutability
Tips
satisfiesis useful for object literals where you want inference plus validation- Prefer
satisfiesover type annotations when you need narrow literal types - For testing type behavior, see python testing with pytest