✅ satisfies Operator in TypeScript
The satisfies operator checks whether a value matches a particular type without changing the value's inferred type.
It was introduced in TypeScript 4.9.
🔹 Basic Example
type User = {
name: string;
age: number;
};
const user = {
name: "Bhaskar",
age: 25
} satisfies User;TypeScript checks that user follows the User type.
If you make a mistake:
const user = {
name: "Bhaskar",
age: "25"
} satisfies User;❌ TypeScript reports an error because age should be a number.
🎯 Why Use satisfies?
The important difference is that satisfies validates the type while preserving the value's specific inferred types.
Compare:
type Config = {
theme: string;
};
const config: Config = {
theme: "dark"
};Here:
config.theme // stringWith satisfies:
const config = {
theme: "dark"
} satisfies Config;TypeScript keeps the more specific inferred type:
config.theme // "dark"🔒 satisfies vs as
These are very different.
Type Assertion
const user = {
name: "Bhaskar"
} as User;You're telling TypeScript:
"Treat this as a
User."
It doesn't properly validate whether the value matches the type.
satisfies
const user = {
name: "Bhaskar"
} satisfies User;You're telling TypeScript:
"Make sure this satisfies the
Usertype."
So:
as → Trust me, this is the type.
satisfies → Check that this matches the type.🧩 Great for Configuration Objects
This is one of the most practical use cases.
type Config = {
apiUrl: string;
timeout: number;
};
const config = {
apiUrl: "https://api.example.com",
timeout: 5000
} satisfies Config;If you accidentally write:
const config = {
apiUrl: "https://api.example.com",
timeout: "5000"
} satisfies Config;❌ TypeScript catches the error.
🔥 satisfies with as const
They work very well together:
type Role = "admin" | "user" | "guest";
const roles = {
admin: "Administrator",
user: "User",
guest: "Guest"
} as const satisfies Record<Role, string>;Here:
as const→ preserves exact values and makes them readonlysatisfies→ checks that all required roles exist and havestringvalues
🧠 Remember
satisfieschecks the shape of a value while preserving its inferred type.
A simple way to remember:
as → "I know what this is."
satisfies → "Check that this is valid."Use satisfies when you want TypeScript to validate an object against a type without losing its specific inferred types.