Some TypeScript types are easy to confuse. Understanding their boundaries is essential for writing safer code.
any: Turning Off Type Checking
any means any type. It is convenient, but it bypasses type checking.
let value: any = 42;
value = "hello";
value.toFixed();More dangerously, any can be assigned to any other type.
let input: any = 9;
let name: string = input; // compiles, but unsafeUse any sparingly, mainly when migrating legacy code or dealing with truly unknown external data.
unknown: A Safer Unknown Value
unknown also represents an uncertain value, but it requires narrowing before use.
let value: unknown = "hello";
if (typeof value === "string") {
console.log(value.toUpperCase());
}If you mean "I do not know what this is yet", prefer unknown over any.
void: No Meaningful Return Value
void is commonly used for functions whose return value should not be used.
function log(message: string): void {
console.log(message);
}never: No Normal Completion
never means a function never returns normally, often because it throws or loops forever.
function fail(message: string): never {
throw new Error(message);
}It is also useful for exhaustive checks.
type Status = "success" | "error";
function handle(status: Status) {
switch (status) {
case "success":
return "ok";
case "error":
return "failed";
default:
const exhaustive: never = status;
return exhaustive;
}
}null and undefined
undefined usually means a value has not been assigned. null usually means intentionally empty.
With strictNullChecks, they cannot be assigned freely to other types.
let age: number | null | undefined;
age = 18;
age = null;
age = undefined;Strict null checking helps prevent many null-reference bugs.
Deeper Notes
When reviewing this topic, do not memorize names only. Focus on any, unknown, void, never, null, undefined, and safety boundaries around narrowing. If this stays at the definition level, it becomes hard to explain in interviews or apply in projects. A stronger way to study it is to place it in a concrete scenario: who calls it, where the input comes from, what happens on failure, and whether data or state can be processed twice.
- TypeScript moves uncertainty from runtime to development time, but it does not replace runtime validation.
- Type design should model business constraints first instead of showing off complex generics.
- For third-party input, JSON, forms, and API responses, combine types with narrowing or schema validation.
In a real project, use it as a decision framework: identify inputs, constraints, failure modes, and observability before choosing a specific tool or pattern. If a solution looks simple, keep asking whether it still works when scale grows, permissions change, recovery matters, and more people collaborate on it.
Practical Checklist
- Identify where this concept sits in the system: development-time constraint, runtime behavior, infrastructure capability, or collaboration workflow.
- Write one minimal working example and one failure example; only knowing the happy path is usually not enough.
- Record common misuses: edge cases, permission assumptions, performance assumptions, sync/async differences, or environment differences.
- Connect the concept to a project experience so that an interview answer can be grounded in real tradeoffs.
- End with one sentence about tradeoff: what it gives up and what it buys.
Self-Check Questions
- What core problem does this topic solve?
- What alternatives exist, and what are their costs?
- Where are the most likely edge cases?
- How would code, tests, or monitoring prove that it is reliable?
Applied Scenario
A practical scenario is a project with shared frontend/backend types. An API returns JSON; the frontend validates it with a schema, then passes the validated data into TypeScript types. Types improve editor feedback and compile-time checking, but they cannot guarantee that network data is valid. Good type design expresses business constraints such as roles, states, permissions, request parameters, and response shapes instead of turning everything into string or any.
Common Pitfalls:
- Treating TypeScript types as runtime validation.
- Using any for convenience and pushing errors into production.
- Making generics so complex that callers cannot understand them.