Google News
logo
TypeScript - Interview Questions
What is any type, and when to use it?
There are times when you want to store a value in a variable but don’t know the type of that variable in advance. For example, the value is coming from an API call or the user input. The ‘any’ type allows you to assign a value of any type to the variable of type any.
let person: any = "Foo";
Here is an example that demonstrates the usage of any type.
// json may come from a third-party API
const employeeData: string = `{"name": "Ramana", "salary": 90000}`;

// parse JSON to build employee object
const employee: any = JSON.parse(employeeData);

console.log(employee.name);
console.log(employee.salary);
TypeScript assumes a variable is of type any when you don’t explicitly provide the type, and the compiler cannot infer the type from the surrounding context. 
Advertisement