TypeScript any type can store all the possible JavaScript value like primitive types(string, Boolean, null, undefined, number, symbol), objects, array, functions, errors etc that means you can assign anything to it. TypeScript any type support from first release.
See some examples of any types.
let any_type_variable: any; any_type_variable = true; // Assign the boolean value console.log(any_type_variable); // output : true any_type_variable = 101; // Assign the number value console.log(any_type_variable); // output : 101 any_type_variable = "Hello TypeScript"; // Assign the string value console.log(any_type_variable); // Hello TypeScript any_type_variable = []; // Assign the array console.log(any_type_variable); // [] any_type_variable = {}; // Assign the object value console.log(any_type_variable); //{} any_type_variable = Math.min; // Assign the min value console.log(any_type_variable); //[Function: min] any_type_variable = null; // Assign the null value console.log(any_type_variable); // null any_type_variable = undefined; // Assign the undefined value console.log(any_type_variable); // undefined any_type_variable = Symbol("type"); // Assign the Symbol console.log(any_type_variable); // Symbol(type) const pi =3.14; let evenNumber=(number:number) => number %2 ==0; any_type_variable = pi ; // Assign the Symbol value console.log(any_type_variable); // 31.4 any_type_variable = evenNumber ; // Assign the function console.log(any_type_variable(20)); // true
In above code snippet, we can assign any type of values to any type variable and perform the operation on it.
let any_type_variable; any_type_variable = true; // Assign the boolean value console.log(any_type_variable); any_type_variable = 101; // Assign the number value console.log(any_type_variable); any_type_variable = "Hello TypeScript"; // Assign the string value console.log(any_type_variable); any_type_variable = []; // Assign the array console.log(any_type_variable); any_type_variable = {}; // Assign the object value console.log(any_type_variable); any_type_variable = Math.min; // Assign the min value console.log(any_type_variable); any_type_variable = null; // Assign the null value console.log(any_type_variable); any_type_variable = undefined; // Assign the undefined value console.log(any_type_variable); // any_type_variable = new TypeError(); // Assign the TypeError value // console.log(any_type_variable); any_type_variable = Symbol("type"); // Assign the Symbol console.log(any_type_variable); const pi = 3.14; let evenNumber = (number) => number % 2 == 0; any_type_variable = pi; // Assign the Symbol value console.log(any_type_variable); any_type_variable = evenNumber; // Assign the function console.log(any_type_variable(20));
You can use the any type when you are not sure about datatype. For example, you have integrated the coupon system from different vendor which use different type of coupon format. Vendor one is using string format, vendor two is using number format, vendor three is using alpha-numeric format and so on….
let coupon_nr: any; coupon_nr = 111222222; coupon_nr = 'COUPON-121'; coupon_nr = 'COUPON91';