Course contentsShow
← TypeScript
Lesson 139 of 1,7963. Basic TypesFree lesson

Comparing any, unknown, and never

Contrast these three special types: their assignability, safety, and appropriate use cases.

Comparing any, unknown, and never

You'll learn how these three special types differ in assignability, safety, and when to use each one.

The Type Safety Spectrum

Think of these three types as representing different levels of type safety:

any is the escape hatch — it accepts anything and can be assigned to anything. It's like removing your seatbelt: you have total freedom but no protection. TypeScript stops checking entirely.

unknown is the safe container — it accepts anything but requires you to check what's inside before using it. It's like a locked box: you can receive any package, but must verify its contents before opening.

never is the impossibility marker — nothing can be assigned to it (except never itself), and it can be assigned to anything. It represents code paths that never produce a value.

Assignability Summary

let anyValue: any;
let unknownValue: unknown;
let neverValue: never;

// any accepts everything
anyValue = 5;
anyValue = "hello";

// unknown accepts everything
unknownValue = 5;
unknownValue = "hello";

// never accepts nothing
// neverValue = 5;  // Error!

When to Use Each

  • any: Only when migrating JavaScript or dealing with truly dynamic data where types are impossible to know
  • unknown: When receiving untyped data (API responses, user input) that you'll validate before using
  • never: For exhaustive checks, functions that throw, or impossible type intersections

Key Takeaway: Prefer unknown over any for type-safe handling of uncertain data. Use never to represent impossibility. Avoid any unless absolutely necessary—it surrenders all type safety.