Lesson 102 of 296
any vs unknown vs never
What you will learn
- What
anydoes and why it is dangerous unknown: the safe alternativenever: the type with no valuesvoidvsnever
On this page
any: turns type checking off
A value of type any can be used in any way and TypeScript checks nothing.
let data: any = JSON.parse('{"name":"Sam"}');
data.toUpperCase(); no error, crashes at runtime
data.foo.bar.baz(); no error
const n: number = data; no errorany also spreads: anything you get from an any is also any, so one any can silently remove type safety from a large part of the code.
Where any appears without you writing it:
JSON.parsereturnsany.- Parameters without types, if
noImplicitAnyis off (Lesson 105). - Some older libraries.
unknown: safe "I do not know yet"
unknown also accepts any value, but you cannot use it until you check what it is (Lesson 106).
let input: unknown = JSON.parse(text);
input.toUpperCase(); Error: 'input' is of type 'unknown'.
if (typeof input === "string") {
input.toUpperCase(); fine: TypeScript now knows it is a string
}any | unknown | |
|---|---|---|
| Accepts any value | Yes | Yes |
| Can be used freely | Yes, no checks | No, you must narrow it first |
| Can be assigned to other types | Yes | Only to unknown and any |
| Safe | No | Yes |
Rule: use unknown for values you do not know yet (API data, parsed JSON, errors in catch blocks), then check them. In strict mode the error in catch (error) is unknown by default:
try {
riskyThing();
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
}
}When is any acceptable?
Rarely: migrating a large JavaScript codebase step by step, or a third-party value you cannot type yet. Mark it so it can be fixed later, and enable the ESLint rule that warns about any.
never: no possible value
never is the type of values that can never exist.
A function that never returns:
function fail(message: string): never {
throw new Error(message);
}An impossible case after all options are handled:
type Shape = "circle" | "square";
function area(shape: Shape) {
if (shape === "circle") return 1;
if (shape === "square") return 2;
shape; type here: never (no options left)
}This is the basis of exhaustive checks (Lesson 108).
Impossible intersections become never:
type Impossible = string & number; nevervoid vs never
void | never | |
|---|---|---|
| Meaning | Returns, but with no useful value | Never returns at all |
| Example | A function that logs a message | A function that always throws or loops forever |
Common mistakes
- Using
anyto make an error go away. - Using
anyfor API responses instead ofunknownplus validation. - Confusing
voidandnever.
How to say it in an interview
"any disables type checking and spreads silently, so I avoid it. unknown also accepts any value, but forces me to narrow it with checks before use, so I use it for untrusted input like parsed JSON and caught errors. never is the type with no values: the return type of functions that always throw, and what remains after every case of a union has been handled, which enables exhaustive checking. void means a function returns but without a useful value."
Key words
| Word | Meaning |
|---|---|
| any | Turns off type checking for a value |
| unknown | Any value, but must be checked before use |
| never | A type with no possible values |
| Top type | A type that accepts everything (unknown) |
| Bottom type | A type with no values (never) |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.