2017-07-27 262 views
4

我的代码TS汇编 - “noImplicitAny”不工作

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

和我tsconfig.json是:

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

但是,什么是地狱它是有throgh编译没有异常js?

最好的问候, Crova

回答

1

noImplicitAny的字面意思是:

触发如果打字稿使用错误 '任何' 时,它不能推断 型

你的情况以上在你的代码编译器的任何一点都可以很容易地推断出z的类型。因此它可以检查是否允许您拨打z的适当方法/道具。

4

因为z从未输入为anyz的类型根据您分配的内容简单推断出来。

release notes

随着打字稿2.1,而不是只选择任何,打字稿会根据你最终后来分配 推断类型。

例子:

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(); 
你的情况

所以:

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 
+0

是否有禁止此行为的标志? – user7353781

+0

没有据我所知。 – Saravana