The any type in TypeScript

Hi, in this post we are going to talk about the “any” type in TypeScript.

Some people say that “any” is the Godfather of types. “any” can do anything for a price, but we prefer not use “any” unless we are completely out of options.

We only use this type when we can’t figure out what type something is, this means that “any” type is our last resort type, and we should avoid it when possible.

Type is a set of values and the things that you can do with them and when we use “any” type this comes to be the set of all values and we can do anything with “any.

Examples of the use of “any“:

let age: any = 31; // any type


let name: any = ["cristina"]; // any type


let data: any = age + name; // any type

If we hover over the data variable we can see how also the type is any.

Note:

By default TypeScript is permissive this means that is permitted to use “any” type in our code, but if we want that TypeScript complain about the use if “any” type then we need to enable this property in our tsconfig.json:

"noImplicitAny": true

By Cristina Rojas.