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 runtimeAll lessons in this course
- Understanding unknown vs any
- The never Type and Impossible States
- The void Type in Functions
- Type-Safe Handling of unknown