2017-07-27 5 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に?

よろしく、 クローヴァ

答えて

1

noImplicitAny文字通り:

トリガエラーそれはあなたのケースでは タイプ

を推測することはできませんいつでも活字体は、 '任意の' を使用している場合あなたのコードコンパイラの任意のポイントで上記のタイプは、zのタイプが簡単に推測されます。したがって、あなたはzで呼び出している適切なメソッド/小道具が許可されているかどうかをチェックすることができます。

4

zは決してanyと入力されません。 zのタイプは、あなたが割り当てたものに基づいて単純に推測されます。 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

関連する問題