Safer primitives for everyday work: • unknown: like a locked box—must narrow before use (safer than any) • never: impossible value; signals functions that throw/loop or exhaustive checks • void: no useful return; usually only undefined allowed • object: any non-primitive value (arrays, functions, objects) Prefer unknown over any for untrusted inputs, and use never to model impossible states. **Why it matters:** • unknown forces you to prove safety before property access—great for untrusted inputs • never catches missing cases in switches so refactors fail fast • void/object communicate intent (fire-and-forget vs non-primitive)
let input: unknown;
function fail(msg: string): never { throw new Error(msg); }function parseUser(input: unknown) {
if (typeof input === "string") {
return { name: input.toUpperCase() };
}
throw new Error("Expected string");
}
function fail(message: string): never {
throw new Error(message);
}
function onClick(): void {
// return undefined implicitly
}
function takesObject(value: object) {
// disallows primitives
console.log(value);
}Understanding Safety Types: unknown, never, void, object is fundamental to mastering TypeScript. Practice with the examples above and experiment with variations to solidify your knowledge.