What are unknown, any and never?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn What are unknown, any and never?
Definition:
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.
Key points:
-
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.
Interview point:
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?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.