any vs unknown vs never

any disables type checking entirely, unknown accepts any value but forces you to narrow it before use, and never is the empty type for values that can never exist — together they are TypeScript's three "special" types.

Learn any vs unknown vs never in our free TypeScript course — an interactive lesson with runnable examples, a practice exercise and a quick reference.

Part of the free TypeScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Imagine a sealed package arrives. any is ripping it open and using whatever's inside without looking — sometimes fine, sometimes you grab a knife by the blade. unknown is putting the package through an X-ray first: you can't touch the contents until the scan tells you what they are. never is a package that, by the laws of physics, could never have been sent — if your code expects one, something upstream is broken.

1. any : The Off Switch

A value typed any tells TypeScript to stop checking it completely. You can call any method, read any property, and reassign it to anything — including operations that don't exist and will crash at runtime. Worse, any is contagious : anything you derive from an any value is also any , so a single careless annotation can quietly disable safety across a whole file.

2. unknown : Safe by Default

unknown is what any should have been. You can assign any value to an unknown , but you cannot use it — no property access, no method calls — until you've narrowed it to a concrete type with typeof , instanceof , Array.isArray , or a custom type guard. This makes it the correct type for anything you don't control: parsed JSON, network responses, third-party callbacks.

3. never : The Impossible Type

never represents a value that can never occur. It's the return type of functions that always throw or loop forever, and — most usefully — it powers exhaustiveness checking . By assigning the leftover value in a switch default to a never variable, you get a compile-time error the moment you add a new union member and forget to handle it.

🎯 Your Turn

Here's the core unknown skill: narrow before you use. Fill in the two blanks marked ___ , then run it.

No blanks this time — just a brief and a starting outline. Build the safe parser yourself, run it, and check your output against the example in the comments.

Practice quiz

What does typing a value as any do to type checking?

  • Makes the value read-only
  • Forces a narrowing check before use
  • Turns OFF type checking for that value
  • Restricts it to objects

Answer: Turns OFF type checking for that value. any tells TypeScript to stop checking - you can call any method or reassign it freely, even operations that crash at runtime.

Why is any described as contagious?

  • Anything you derive from an any value is also any
  • It throws errors that spread across files
  • It automatically converts other types to any
  • It disables the whole project's type checking

Answer: Anything you derive from an any value is also any. Values derived from an any (like let len = x.length) are themselves any, so a single any quietly disables safety everywhere it spreads.

What must you do before using a value typed unknown?

  • Cast it to any first
  • Nothing - unknown behaves like any
  • Wrap it in a Promise
  • Narrow it to a concrete type with a check

Answer: Narrow it to a concrete type with a check. unknown accepts any value but blocks every operation until you narrow it with typeof, instanceof, Array.isArray, or a type guard.

Which type is the right choice for untrusted data like parsed JSON?

  • any
  • unknown
  • never
  • void

Answer: unknown. unknown keeps the safety net for data you don't control - you must prove its shape before trusting any field.

What is the never type?

  • The empty type - no value can ever have it
  • A value that is always undefined
  • Another name for null
  • A type for empty arrays only

Answer: The empty type - no value can ever have it. never is the empty type: no value can ever be a never. It's the return type of functions that throw or loop forever.

What is the return type of a function that always throws an error?

  • void
  • undefined
  • never
  • unknown

Answer: never. A function that never returns normally (it always throws or loops) has return type never.

In a switch, what does assigning the leftover value to a never variable in the default branch achieve?

  • It speeds up the switch at runtime
  • An exhaustiveness check - it errors if a case is unhandled
  • It converts the value to a string
  • It silences all type errors

Answer: An exhaustiveness check - it errors if a case is unhandled. If you add a new union member and forget a case, the value is no longer assignable to never, so TypeScript flags the gap.

Inside if (typeof value === "string") { ... }, what is value's type in that block?

  • unknown
  • any
  • never
  • string

Answer: string. Control-flow narrowing makes TypeScript treat value as string inside the branch, so string operations become allowed.

Which compiler flag helps keep accidental any out of your code?

  • strictNullChecks
  • noImplicitAny
  • noUnusedLocals
  • skipLibCheck

Answer: noImplicitAny. noImplicitAny makes TypeScript error when it would otherwise silently fall back to any, flushing out accidental any.

When you write an empty array literal, what element type can TypeScript infer in some positions?

An empty array can infer as never[] in some positions; annotate it (const xs: string[] = []) to avoid confusing 'not assignable to never' errors.