0Pricing
TypeScript Academy · Lesson

Understanding unknown vs any

Why unknown is the type-safe alternative to any.

Two Top Types

TypeScript has two types that can hold any value: any and unknown. They look similar but behave very differently. Choosing the right one is a core skill for type-safe code.

let a: any = 5;
let u: unknown = 5;
console.log(a, u);

any Disables Type Checking

any is an escape hatch that turns off the type checker for that value. You can access any property or call it like a function — the compiler stays silent, even when it would crash at runtime.

const a: any = 'hello';
console.log(a.toUpperCase()); // OK
// a.doesNotExist(); // no compile error, but crashes at runtime

All lessons in this course

  1. Understanding unknown vs any
  2. The never Type and Impossible States
  3. The void Type in Functions
  4. Type-Safe Handling of unknown
← Back to TypeScript Academy