any vs unknown vs never in TypeScript
Understand TypeScript any, unknown and never — the differences, type safety, narrowing, exhaustiveness checks, code examples and interview answers.
Expected Interview Answer
any disables type checking entirely, unknown is a type-safe counterpart that accepts any value but forbids using it until you narrow it, and never represents a value that can never occur.
any opts a value out of the type system, so any operation is allowed and safety is lost. unknown accepts anything on assignment but blocks all property access, calls, and other-type assignments until you narrow it with a type guard, making it the safe top type. never is the bottom type with no possible values — it is the return type of functions that always throw or never return, and the result of impossible type combinations, used for exhaustiveness checks.
- unknown preserves type safety while accepting arbitrary input
- never enables compile-time exhaustiveness checking in switch statements
- any is an escape hatch for gradual migration when needed
- Choosing unknown over any catches misuse at compile time
- never documents code paths that should be unreachable
AI Mentor Explanation
any is a batter allowed to swing at anything with no umpire watching — freedom, but disasters go uncaught. unknown is a delivery you must review on replay before deciding your shot; you can't just play blindly. never is a scenario that can't happen, like a batter scoring seven runs off one legal ball with no overthrows — the rulebook simply has no such outcome.
Step-by-Step Explanation
Step 1
Reach for unknown over any
When a value's type is genuinely uncertain, prefer unknown so the compiler forces you to check it.
Step 2
Narrow unknown before use
Apply typeof, instanceof, or a custom type guard to convert unknown into a concrete usable type.
Step 3
Use never for exhaustiveness
In a switch over a union, assign the default case to a never variable so new cases become compile errors.
Step 4
Recognise never return types
Functions that always throw or loop forever are inferred to return never.
Step 5
Limit any deliberately
Confine any to migration boundaries and replace it as types become known.
What Interviewer Expects
- Explaining why unknown is safer than any
- Knowing never is the bottom type with no values
- Demonstrating exhaustiveness checks with never
- Understanding narrowing of unknown via type guards
- Awareness that any silently disables checking
Common Mistakes
- Treating unknown as interchangeable with any
- Trying to access properties on unknown without narrowing
- Thinking never can hold undefined or null
- Using any as a quick fix and leaving it permanently
- Confusing never with void
Best Answer (HR Friendly)
“any turns off the safety checks so anything is allowed, unknown accepts any value but makes you verify what it is before using it, and never marks something that can never actually happen. Preferring unknown over any keeps code safer, and never helps catch missed cases.”
Code Example
let value: unknown = fetchData();
// value.trim(); // Error: object is of type 'unknown'
if (typeof value === 'string') {
console.log(value.trim()); // OK after narrowing
}
type Shape = 'circle' | 'square';
function area(s: Shape): number {
switch (s) {
case 'circle': return 3.14;
case 'square': return 4;
default:
const _exhaustive: never = s; // errors if a case is missing
return _exhaustive;
}
}
function fail(msg: string): never {
throw new Error(msg);
}Follow-up Questions
- Why is unknown considered safer than any?
- How do you narrow an unknown value before using it?
- How does never enable exhaustiveness checking?
- What is the difference between never and void?
- When is it acceptable to use any?
MCQ Practice
1. Which type accepts any value but forbids using it until narrowed?
unknown is the type-safe top type: it accepts anything but blocks operations until you narrow it.
2. What is the return type of a function that always throws?
A function that never returns normally is inferred to return never, the bottom type.
3. What does assigning a switch's default case to a never variable achieve?
If a union member is unhandled, it isn't narrowed to never, producing a compile error that flags the missing case.
Flash Cards
What does any do? — Disables type checking for a value, allowing any operation without safety.
What is unknown? — A type-safe top type that accepts any value but forbids use until narrowed.
What is never? — The bottom type with no possible values; the return of functions that never return.
unknown vs any? — Both accept anything, but unknown forces narrowing before use while any does not.