使用Javascript练习,如果我想测试一段代码
function logic(a, b){
x= a+b;
y = x-5;
console.log(y);
return; // This is my break statement to make sure the function runs until y.
z = y*10; //This line is not executed.
}
现在使用angular2练习打字稿时,如果我使用返回
logic(a, b) => {
x= a+b;
y = x-5;
console.log(y);
return; // Throws error
z = y*10; //I need to comment out this line to avoid error
}
它表示检测到无法访问的代码。有什么方法可以阻止执行。
答案是更新根文件夹中的tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowUnreachableCode": true, //add this line
"lib": [
"es2016"
]
}
}
答案 0 :(得分:0)
是的,设置--allowUnreachableCode
。请参阅:https://www.typescriptlang.org/docs/handbook/compiler-options.html