Daily Shaarli

All links of one day in a single page.

April 24, 2023

Clarifying the `satisfies` Operator | Total TypeScript

When you use satisfies, the value BEATS the type.

The value of the variables is taken into account instead of the type declared in semicolon.

Also colon annotations and satisfies are equally safe.
Also unlike satisfies and colon annotations, using ‘as’ annotations lets you lie to TypeScript.

To recap, we’ve got FOUR ways of assigning a type to a variable:

  • colon annotations
  • satisfies
  • as annotations
  • not annotating and letting TS infer it

The rule of thumb is that you should only use satisfies in two specific situations:

  • You want the EXACT type of the variable, not the WIDER type.
  • The type is complex enough that you want to make sure you didn’t mess it up