any turns off type checking completely. It spreads quickly through your code and hides bugs. Prefer unknown when you don't know a type yet:
// ❌ Dangerous - no checks
function process(data: any) {
return data.value.toUpperCase(); // Might crash!
}
// ✅ Safe - forces checks
function process(data: unknown) {
if (typeof data === "object" && data !== null && "value" in data) {
const obj = data as { value: string };
return obj.value.toUpperCase();
}
}unknown forces you to check a value before using it, making your code safer.
Narrowing techniques:
typeof for primitives – typeof x === 'string'Array.isArray() for arraysin operator for properties – 'name' in objfunction isUser(x: unknown): x is UserIn this category, you'll:
unknown for inputs you need to validateany into your codeany to unknownBy the end, you'll understand why unknown is safer than any and how to work with it confidently.