Open In App

Explain about noImplicitAny in TypeScript

Improve
Improve
Like Article
Like
Save
Share
Report

TypeScript is an object-oriented, strongly-typed, compiled language developed by Microsoft Corporation. It is a superset of javascript which means every sustainable JavaScript code is essentially valid TypeScript code (that means JavaScript is TypeScript with some additional features). TypeScript is built over JavaScript. What we can do is to write TypeScript code and compile it into JavaScript using TypeScript Compiler. Migrating a valid JavaScript codebase into TypeScript is not much of difficult because a syntactically correct JavaScript code is nothing but TypeScript code.

As a prerequisite of understanding noImplicitAny, we need to learn about Any datatype in TypeScript.

What is any type? 

Any type should really be used when we don’t actually know what type will be returned to us. For example, when we are using any function argument which returns anything without a type definition, this is even applicable when it is written in plain JavaScript. Any type is a special type in TypeScript, we can use it when we want to effectively turn off TypeScript Type checking. We often need this because sometimes we need to ignore the type-error given by TypeScript. It is like a Superset of all the built-in and user-defined types in Typescript.

If we have a let statement (a let statement allows us to reassign a value whereas a const does not) and variable identifier text and we don’t add a type to it:

let test;

Here let test transforms into let test: any in our IDE, thus TypeScript is actually inferring the type to us instead of declaring it. However even after we assign a string to the variable test, it will indeed be completely right but still, our IDE will show it as any type. Thus, Typescript doesn’t change the datatype of a variable with any type, it is set as default.

let test;
test = "GeeksForGeeks";

What is noImplicitAny?

The noImplicitAny is a compiler configuration or compiler option that we can modify in TypeScript. The main function of this configuration is by using this we can transform typeScript from optionally-typed to strictly-typed language. If we don’t specify a type, and TypeScript can’t infer it automatically, the compiler will default to any type.  This can be a serious problem in some instances because having any type to a variable tells the compiler “don’t type check this”.  

The existence of the noImplicitAny compiler option will cause cases where any is inferred for a type to become errors.  This will force us to either fix things in such a way that the type can be inferred correctly or at least explicitly declare the type as any type. 

Example:

HTML




<!DOCTYPE html>
<html>
 
<body>
    <pre>
        Here we haven't explicitly declared the variable.
        So a error will be returned.
    </pre>
 
    <script type="text/javascript">
        function printMe(g) {
            console.log(g);
        }
        printMe(g);
    </script>
</body>
 
</html>


When we have configured noImplicitAny in the compiler options the above code will throw an error because we haven’t explicitly declared the variable g

Output:

Example:

HTML




<!DOCTYPE html>
<html>
 
<body>
    <pre>
        Here we have explicitly declared the variable.
        So no error will be returned.
    </pre>
 
    <script type="text/javascript">
        let g;
        function printMe(g) {
            console.log(g);
        }
        printMe(g);
    </script>
</body>
 
</html>


Here we have explicitly declared the variable g, so no error will be returned.

Output:

How to Use noImplicitAny?

For using noImplicitAny as a compiler option we need to configure the compilerOptions in tsconfig.json. We need to set noImplicitAny as true. So by having noImplicitAny set to true we will ensure that we never accidentally let the compiler miss type checking by inferring to any type. After doing this

{
  "compilerOptions": {
    "noImplicitAny": true
  }
}

There is another way of enabling noImplicitAny in compiler options by using the strict option. Using noImplicitAny is a good practice for any developer, no one will want to add datatypes everywhere when migrating from JavaScript to TypeScript codebase. So after we use noImplicitAny we have to explicitly add type at the time of the first scripting only so that it doesn’t become a pain later. 

{
  "compilerOptions": {
    "strict": true
  }
}


Last Updated : 23 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads