In TypeScript, the satisfies
operator is a safer, more precise alternative to as
when you want to ensure a value conforms to a type without losing type inference.
Key Differences
Feature | as |
satisfies |
---|---|---|
Type checking | Not enforced | Enforced |
Keeps extra properties | No | Yes |
Narrows const types |
No | Yes |
Safer for configuration objects | No | Yes |
1. Keeping Extra Properties
Using as
(unsafe):
type Person = {
name: string;
age: number;
};
const user = {
name: 'Alice',
age: 30,
email: '[email protected]',
} as Person; // No error, but `email` is lost from the type
Using satisfies (safe):
const user = {
name: 'Alice',
age: 30,
email: '[email protected]',
} satisfies Person;
// TS checks that name and age exist and retains `email` in the inferred type
2. Narrowing const Types
Using as:
const colors = ['red', 'green', 'blue'] as string[];
Using satisfies:
const colors = ['red', 'green', 'blue'] satisfies readonly ['red', 'green', 'blue'];
3. Ensuring Correct Object Shape
Using as (can lie):
type Config = { port: number };
const config = {
port: '3000',
} as Config; // No error, even though it's wrong!
Using satisfies (type checked):
const config = {
port: '3000',
} satisfies Config; // Type error: 'string' is not assignable to 'number'
4. With Utility Types
type Route = {
path: string;
name: string;
};
const routes = {
home: { path: '/', name: 'Home' },
about: { path: '/about', name: 'About' },
} satisfies Record<string, Route>;
Conclusion
Use satisfies when you want strong type validation without losing inferred details. It's ideal for configuration objects, constant arrays,
Top comments (3)
Would you say there's any benefit to using
satisfies
over a type assignment (or whatever its called...)? For example:vs.
pretty cool seeing type safety get pushed like this tbh - makes me rethink some shortcuts i've taken before
you ever catch something big just because you swapped over to stricter checks
I've noticed that bug reports have dropped to almost zero since we switched from JS to TS. Enough so that I double-checked that our bug tracker was configured correctly after a couple months.
Using well-defined & strict typings have been the major differentiator I think. Not to mention how much more self-documenting it's been.