I’ve recently come across the satisfies
operator in TypeScript and I’m trying to understand how it differs from regular type assertions
. Both seem to provide a way to work with types, but I’m not clear on when to use one over the other.
- How does the satisfies operator enhance type safety compared to type assertions?
- What kind of errors can be caught using satisfies that might be missed with type assertions?
- When should I prefer using satisfies over type assertions and vice versa?
// Satisfies
interface User1 {
username: string;
passcode: number;
}
const user1 = {
username: "joe",
passcode: 2076,
email: "[email protected]"
} satisfies User1;
// Type Assertion
interface User2 {
username: string;
passcode: number;
}
const user2 = {
username: "doe",
passcode: 3000,
email: "[email protected]"
} as Person;