Skip to content

Any type

von Schappler edited this page Nov 18, 2024 · 1 revision

"Special type: Any"

We already know that TypeScript is able to infer the types of assigned variables when we declare those. So the snippet of code below would warn us about typecheck errors because it was infered that name is a string, and so we could not reasign it to a number or try to run array methods over it.

let name = 'bob';
name = 1;
name.map(() => {
  /* do something **/
});

But what would happen if we manually set the type declaration of name to any? Well essentially, we are disabling the typecheck features provided by TypeScript. Even though that sounds a good idea at the begining, that should be avoied as much as possible, because we, as devs, want our codebase to be as type-safe as possible.

When to use the type any?

A short answer to that would be: We shouldn't. Even though it's it's tempting to just manually type everything as any so all the typechecks errors go away, why then use TypeScript in the forst place if we are using the type any?

There is though a particular situation where the use of any as type "is permitted": when we are working on the process of transitioning our codebase from JavaScript to TypeScript and we need a temporary solution for the lack of immediate time to write all the required type checking on the code.

Even though there is a better approach to this, which is using another special type unknkown which will be covered in future sections.

Clone this wiki locally