4 min read
any, unknown, and never
Prerequisites: Union types
Three special types with very different purposes:
any— turns off type checking for that value entirely. Use it only as a last resort; it defeats the entire point of TypeScript wherever it's used.unknown— likeany, it can hold anything, but you must narrow it (check its actual type) before doing anything with it — the safe version of "I don't know this type yet."never— a value that can never actually occur, such as a function that always throws or loops forever.
function process(value: unknown) {
if (typeof value === "string") {
console.log(value.toUpperCase()); // safe: narrowed to string
}
}
Common mistakes
- Reaching for
anyto silence a type error instead of fixing the actual type mismatch — it's a compiler off-switch, and quietly disables checking anywhere it spreads.