What are unknown, any and never?
Try code in the playgroundUnderstand the concept
any allows operations without normal type checking, while unknown accepts any value but requires checks before use. never represents a situation where no value is possible, such as the result of a function that always throws. These types describe different levels of knowledge and should not be used interchangeably.
Important ideas
-
unknown: check the value before using it. -
any: skips type checks and can hide bugs. -
never: no possible value; useful for exhaustive checks or functions that always throw. -
Use
unknownfor external data until you have checked its structure. -
Use
neverin exhaustive switch checks so a newly added union case causes a compile-time error.
Can you explain it without the notes?
What are unknown, any and never?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
any allows operations without normal type checking, while unknown accepts any value but requires checks before use. never represents a situation where no value is possible, such as the result of a function that always throws. These types describe different levels of knowledge and should not be used interchangeably.
Turn this concept into a clear answer.
unknownkeeps the burden of proof on the code using the value;anyremoves that protection.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “What are unknown, any and never?”. Examples are study fragments; framework behavior can depend on your version.