Sub topic 9
any, unknown, never, and void in assignments
Assume you know TypeScript type annotations, interfaces, functions, arrays, and common compiler errors. This chapter focuses on how special types behave when one value is assigned to another type.
The four special types
TypeScript models these types differently from ordinary object or primitive types:
| Type | Main meaning | Assignment behavior |
|---|---|---|
| `any` | Disable most checking | Assignable to almost every type |
| `unknown` | A value whose type is not known yet | Accepts every value, but requires narrowing before use |
| `never` | No value can exist | Assignable to every type |
| `void` | A function result that callers should ignore | Mainly describes missing return values |
`any`: the escape hatch
A value of type any can be assigned to a string, number, object, or function without a compatibility error. Operations on it are also unchecked.
let data: any = "hello";
const count: number = data; // Allowed
data.notARealMethod(); // Allowed by the type checkerThis flexibility can move errors from compile time to runtime. Use any at a boundary only when you have no better type, and contain it as quickly as possible.
`unknown`: safe uncertainty
unknown also accepts every value, but it does not let you use that value as a specific type until you check it.
function printLength(value: unknown): void {
if (typeof value === "string" || Array.isArray(value)) {
console.log(value.length);
}
}
const input: unknown = "TypeScript";
printLength(input);You can assign unknown to unknown or any, but not directly to string, number, or another specific type. This makes unknown a safer choice for parsed data or external input.
`never`: impossible results
never describes a computation that cannot produce a value. A function that always throws or never finishes can return never.
function fail(message: string): never {
throw new Error(message);
}
const result: string = fail("No result exists");The assignment works because a never value can be assigned to every other type. The reverse is not true: a string or number cannot be assigned to never.
`void`: ignored function results
void usually describes a function whose result is not intended for callers.
const logMessage: (message: string) => void = (message) => {
console.log(message);
};
logMessage("Saved");A function that returns a value can also be assigned to a void-returning function type. The caller is allowed to ignore that value.
const callback: () => void = () => 42; // AllowedThis special rule applies to function assignment. It does not mean that a number is assignable to a variable of type void.
A useful mental model
Practice
When an external JSON value has not been validated, type it as unknown. After checking its shape, assign it to a specific interface. Use never for impossible branches and void for callbacks whose results are intentionally discarded.
Exercise
Which type is safer than any for unvalidated input, and what must you do before treating its value as a string?
Show answer
Use unknown. Narrow it with a check such as typeof value === "string" before string operations.