as
vs type annotations vs satisfies
We have now learned 3 ways to denote types:
- Type annotations (e.g.
const x: string
) - Type assertions (
as
) satisfies
What are their differences and when do we use them?
Try to use type annotations as much as possible. Use satisfies
when you need to retain the inferred type of your variable. Never use type assertions, if possible.
Exercise 8.1
Refactor the provided code to use type annotations, "satisfies", and "as const" appropriately, and observe the differences in inferred types.