Well, well if you wonder what is the difference between any and unknown in typeScript this article is for you.
Any
In TypeScript, the any type is used to represent a value of any type. It essentially tells the compiler to temporarily suspend type checking and allow any operations on the value without generating compile-time errors. While any can be useful in certain scenarios, it should generally be used sparingly, as it undermines the benefits of static typing provided by TypeScript.
In short words: Any type allow you to do anything you want without and disable type checking.
❌ Avoid using any as a type unless you dont have another option.
Example
let value: any;
value = true; // OK
value = 42; // OK
value = "Hello World"; // OK
value = []; // OK
value = {}; // OK
value = Math.random; // OK
value = null; // OK
value = undefined; // OK
value = new TypeError(); // OK
value = Symbol("type"); // OK
value.foo.bar; // OK
value.trim(); // OK
value(); // OK
new value(); // OK
value[0][1]; // OK
Unknown
Unknown type is used to represent values whose type is unknown at compile-time. It provides a safer alternative to the any type by enforcing type checking and preventing unsafe operations without explicit type assertions or checks.
The unknown type is only assignable to the any type and the unknown type itself.
Here are some key points to understand about the unknown type:
Type Safety: Unlike the any type, which allows any operations on a value without type checking, the unknown type requires you to perform explicit type checks or type assertions before using the value. This ensures that you handle unknown values in a type-safe manner.
Type Inference: When a value is assigned to a variable of type unknown, TypeScript does not infer any specific type information about that value. Essentially, an unknown value can be anything, and the compiler restricts you from making any assumptions about its structure or behavior until you narrow down its type.
Type Guarding: To safely use an unknown value, you can use type guards, such as typeof, instanceof, or custom type predicates, to narrow down its type within a specific code block. This allows you to access properties, call methods, or perform other operations that are valid for a specific type.
Type Assertions: If you have additional information about the type of an unknown value, you can use type assertions (also known as type casting) to tell the TypeScript compiler the specific type of the value. This allows you to perform operations specific to that type. However, type assertions should be used with caution, as they can lead to runtime errors if the assertion is incorrect.
Compatibility: The unknown type is compatible with all other types in TypeScript. You can assign an unknown value to any type without a type error, but you'll still need to perform appropriate type checks or assertions before using the value.
const value: unknown = "Hello World";
const someString: string = value as string;
const otherString = someString.toUpperCase(); // "HELLO WORLD"
function printLength(value: unknown) {
if (typeof value === 'string') {
console.log(value.length); // Type-safe: value is narrowed down to string
} else {
console.log('Value is not a string');
}
}
printLength('Hello'); // Output: 5
printLength(42); // Output: Value is not a string
Example:
let value: unknown;
let value1: unknown = value; // OK
let value2: any = value; // OK
let value3: boolean = value; // Error
let value4: number = value; // Error
let value5: string = value; // Error
let value6: object = value; // Error
let value7: any[] = value; // Error
let value8: Function = value; // Error
value.foo.bar; // Error
value.trim(); // Error
value(); // Error
new value(); // Error
value[0][1]; // Error
The unknown type is particularly useful when dealing with scenarios where the type of a value is not known in advance, such as parsing dynamic data or handling user input. By using unknown, you can enforce type safety and ensure that your code handles uncertain or untrusted values in a controlled manner.
If you like my work and want to support me to work hard, please donate via:
Revolut website payment or use the QR code above.
Thanks a bunch for supporting me! It means a LOT 😍
Latest comments (0)