TS compilation - "noImplicitAny" does not work

I have a code

let z;
z = 50;
z = 'z';

and my tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": false,
    "noEmitOnError": true,
    "strict": true,
    "noImplicitAny": true
  }
}

But what the hell are there no exceptions related to js compilation?

Best regards, Krova

+4
source share
2 answers

Because it is znever entered as any. The type is zsimply inferred based on what you assigned to it.

From the release note :

With TypeScript 2.1, instead of simply choosing TypeScript, it 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:

let z;
z = 50;
let y = z * 10; // `z` is number here. No error
z = 'z';
z.replace("z", "")// `z` is string here. No error
+4
source

noImplicitAny literally means:

, TypeScript "any",

, z. , /, z.

+1

Source: https://habr.com/ru/post/1682409/


All Articles