The difference between any and unknown type in TypeScript
Both any
and unknown
are universal types in TypeScript that allow you to assign whatever the heck you want to it.
let exampleAny: any;
let exampleUnknown: unknown;
// I can assign anything to `any` type
exampleAny = {}
exampleAny = 123;
// I can also assign anything to `unknown` type
exampleUnknown = {}
exampleUnknown = 123;
So you may be asking… what’s the difference between them? The difference is in accessing those values. Look!
let exampleAny: any;
let exampleUnknown: unknown;
// You can access Number.prototype method on any
// Without checking if the value is a number
console.log(exampleAny.toFixed());
// You can't access Number.prototype method on unknown
// Without checking if the value is a number
console.log(exampleUnknown.toFixed());
// ‼️ Error: Object is of type 'unknown'
Using any
disables type-checking same as @ts-ignore
does. The unknown
is a type-safe counterpart of any
type — before accessing the value, it requires type assertion or narrowing to a more specific type.
// Thats fine because before accesing Number.prototype method
// We can be assured that the type of value is a "number"
if (typeof exampleUnknown === "number") {
console.log(exampleUnknown.toFixed());
}
Rule of thumb for any and unknown type
Use any
type only during the migration from JavaScript to TypeScript codebase, unknown
otherwise. Karma will catch you if you assign an any
type just because you can’t bothered to define meaningful type!
Thanks for this clarity
I am glad it helped you out :)