The no Compiler Options
Learn about several compiler options that are prefixed with the word no, and understand their functionality.
There are also a number of compiler options that are prefixed with the word no. These options are similar to the strict options in that they further guard our code against things like unused parameters, implicit returns, and implicit any.
In this lesson, we will take a look at these compiler options and how they
can detect potential errors within our code. These parameters are similar in nature to strict parameters in that they can be turned on or off and can be introduced into a code base gradually.
Note: If the
strictcompiler option has been set totrue, then all of these options will be enabled as well.
The noImplicitAny option
When a type has not been specified for a property, parameter, or function return
type, the TypeScript compiler will automatically assume that it is of type any. In strict mode, this will generate an error.
Consider the following code:
declare function testImplicityAny();
Here, we have a function declaration named testImplicitAny, which will generate an error. What this error indicates is that the testImplicitAny function has not specified a return type and, therefore, will return a type of any.
This can be fixed by specifying a return type as follows:
declare function testImplicityAny(): void;
Here, we specify that the testImplicityAny function returns a type of void, and the error is removed.
If a function parameter or a class property has not specified a type, then similar errors are generated.
Consider the following code:
function testNoParamType(value) { }class TestAny {id;}
Here, we have a function named testNoParamType that has a single parameter named value. The value parameter does not have a type specified.
We also have a class named TestAny that has a single property named id and also does not have a type specified.
This code will produce two errors.
When we run the code, we can see that the compiler is identifying both the value parameter and the id property as implicitly being of type any.
These errors can be fixed by specifying a type in each of these cases:
function testNoParamType(value: string) { }class TestAny {id : any;}
Here, we modify the function signature of the testNoParamType function to ...