Skip to the content

Lesson 102 of 296

any vs unknown vs never

What you will learn

  • What any does and why it is dangerous
  • unknown: the safe alternative
  • never: the type with no values
  • void vs never
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 error

any 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.parse returns any.
  • Parameters without types, if noImplicitAny is 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
}
anyunknown
Accepts any valueYesYes
Can be used freelyYes, no checksNo, you must narrow it first
Can be assigned to other typesYesOnly to unknown and any
SafeNoYes

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;       never

void vs never

voidnever
MeaningReturns, but with no useful valueNever returns at all
ExampleA function that logs a messageA function that always throws or loops forever

Common mistakes

  • Using any to make an error go away.
  • Using any for API responses instead of unknown plus validation.
  • Confusing void and never.

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

WordMeaning
anyTurns off type checking for a value
unknownAny value, but must be checked before use
neverA type with no possible values
Top typeA type that accepts everything (unknown)
Bottom typeA type with no values (never)

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.