What is the scale of the noImplicitAny compiler flag
TypeScript documentation documents a flag noImplicitAny
for the compiler
Increase the margin of error in expressions and declarations with an implied type
any
.
So in the following code:
let x; // x is of implicitly of type `any`, but no error
function foo(y) { // error: parameter 'y' implicitly has an 'any' type.
let z; // z is of implicitly of type `any`, but no error
}
Should not mark x
and z
as an implicit imposition on any
?
source to share
This is actually due to a fix made in version 2.1. Before that, your code might have caused errors.
From the release notes :
As of TypeScript 2.1, instead of just choosing, TypeScript will infer types based on what you end up assigning later.
Example:
let x; // You can still assign anything you want to 'x'. x = () => 42; // After that last assignment, TypeScript 2.1 knows that 'x' has type '() => number'. let y = x(); // Thanks to that, it will now tell you that you can't add a number to a function! console.log(x + y); // ~~~~~ // Error! Operator '+' cannot be applied to types '() => number' and 'number'. // TypeScript still allows you to assign anything you want to 'x'. x = "Hello world!"; // But now it also knows that 'x' is a 'string'! x.toLowerCase();
So, in your case, TypeScript will actually infer the types based on what you've assigned to it:
function foo(y) {
let z;
z = "somestring";
z.toUpperCase(); // z is string now. No error;
z = 10;
z.toUpperCase(); // z is number now; Error
}
source to share