What is TypeScript Data Type – Any

TypeScript has type-checking and compile-time checks. However, we do not always have prior knowledge about the type of some variables, especially when there are user-entered values from third party libraries. In such cases, we need a provision that can deal with dynamic content. The Any type comes in handy here.Example: Any Copy

let something: any = "Hello World!"; 
something = 23;
something = true;

The above code will compile into the following JavaScript.

var something = "Hello World!";
something = 23;
something = true;

Similarly, you can create an array of type any[] if you are not sure about the types of values that can contain this array.Example: Any type Array Copy

let arr: any[] = ["John", 212, true]; 
arr.push("Smith"); 
console.log(arr); //Output: [ 'John', 212, true, 'Smith' ] 

The above example will generate the following JavaScript code:

var arr = ["John", 212, true];
arr.push("Smith");
console.log(arr);

Post Author: admin

Avatar
I am Software Engineer, Microsoft MVP for Developer Security and ASPInsider. I am regular conference speaker, frequent blogger at learnfreecoding.com and author of the OWASP Top 10 for .NET. I worked for lots of companies throughout the last decade and is keen to share my knowledge with the community. I have broad and deep knowledge of the Microsoft stack with a special interest on web technology and the cloud. for consultancy email me on info@learnfreecoding.com