any, unknown, never
any opts out
The any type switches the checker off for one variable. Any value fits and any operation is allowed, so errors move back to run time where they are harder to find:
let loose: any = 5; loose = "now text"; console.log(loose);
now text
The snippet compiles and prints without complaint. Reach for any only at the border with untyped code, and replace it with a precise type as soon as the shape is known.
unknown is honest
The unknown type also accepts every value, but it refuses to let you use the value before a check. That single demand removes the danger that makes any risky:
function read(value: unknown): string { if (typeof value === "string") { return value; } return "not text"; } console.log(read("hi"));
hi
Inside the if branch, typeof has proven the value is a string, so returning it is safe. Outside the branch the value is still unknown, and only the fallback text is returned.
never happens
The never type marks code that cannot produce a value because it never returns. Functions that always throw errors are the classic case:
function fail(message: string): never { throw new Error(message); }
Nothing can be assigned from fail because there is nothing to assign: the throw ends the call every time. The compiler uses never to prove that all paths of a check were handled.
Which to reach for
Default to unknown for values of unclear shape, keep never for functions that cannot return, and leave any for quick migration drafts. The pattern below shows the honest habit in three lines:
const input: unknown = "data"; if (typeof input === "string") { console.log(input.toUpperCase()); }
DATA
Accept the value as unknown, prove it with typeof, and only then call string methods. That order keeps the compiler as your reviewer instead of a silent witness.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the type
anydo to the checker?Answer
It turns the checks off for that value
anyaccepts every value and every operation, so mistakes move from the compile to the run. -
Why is
unknownsafer thanany?Answer
It accepts every value but only allows use after a check
The compiler demands a check such as
typeofbefore the value is used, so the danger of a blind call goes away. -
What does this program print?
let loose: any = 5; loose = "now text"; console.log(loose);Answer
now text
anyallows the second assignment, so the variable holds the string when it is printed. -
What does this program print?
function read(value: unknown): string { if (typeof value === "string") { return value; } return "not text"; } console.log(read(42));Answer
not text
The value is a number, so the fallback line returns
not text. -
What does the compiler say about this program?
const value: unknown = "hi"; console.log(value.toUpperCase());Answer
error TS18046: 'value' is of type 'unknown'.
An unknown value cannot be used before a check proves its type, and the error names the value.
-
What does
neversay about a function?Answer
The function cannot return normally
A
neverfunction always throws or never ends, so it produces no return value at all. -
What does this program print?
function read(value: unknown): string { if (typeof value === "number") { return "a number"; } return "something else"; } console.log(read(7) + " / " + read("seven"));Answer
a number / something else
The number takes the first branch and the string falls through to the last return.
-
What does this program print?
const loose: any = "5"; const n: number = loose; console.log(n + 1);Answer
51
anylets the string through, and at run time+joins it with 1 instead of adding. -
Which type fits a value whose shape is not known yet?
Answer
unknownunknownkeeps the compiler watching until a check proves the shape;anywould silence it. -
A function is declared with the return type
neverand the body ends withthrow new Error("stop"). What does a call to it produce?Answer
No value: the call always ends with the thrown error
The throw ends every call, which is exactly what
neverpromises the compiler.
Article author: Arthur Isaev