TypeScript to introduce the "satisfies" Operator
#51 · 2022-09-24 · TypeScriptTypeScript 4.9 beta introduces an interesting new feature: the satisfies
operator.
The release notes show an example where that can be useful.
Generally, it makes it easy to type hint values inline, for example when defining a (complex) object.
satisfies
is similar to type assertions or the as
operator.
But unlike with these two approaches, TypeScript will not blindly accept anything when you use satisfies
.
It still enforces the type to be correct.
Refer to the following example (playground).
type A = 'A';
const obj: Record<string, string> = {
// TypeScript will allow this, although it is clearly wrong
prop1: <A>'B',
// TypeScript will allow this, although it is clearly wrong
prop2: 'B' as A,
// TypeScript will throw an error:
// Type '"B"' does not satisfy the expected type 'A'.(1360)
prop3: 'B' satisfies A,
}
More information about the satisfies
operator can be found in the following tickets.