DEV Community

Discussion on: A Deep Dive into the satisfies operator in Typescript

Collapse
 
anodynos profile image
Angelos Pikoulas • Edited

What is the benefit compared to const circle: Circle = {...} ? Or const helloStr: string = 'hello' and so on?

Collapse
 
mainawycliffe profile image
Maina Wycliffe

As explained, explicit type annotation ties the type of variable to the annotation, i.e. helloStr can now only be a string, instead of the narrower literal type "hello" and if you wanted to use it in a place that requires the type "hello" (not string), you would need to narrow it down to hello (by checking if it's hello before passing it along).

In contrast, when using satisfies, you get the same type-safety that helloStr can only be a string, but the inferred narrower type of hello is retained, hence we are just checking to make sure that our variable type fits a certain type (in this case can only a string) without changing its narrower or more precise inferred type. For const helloStr, the type hello is more precise and much a better type than string.

This is because you should always prefer the narrower variable type whenever possible (link).