typescript 为什么 TSLint 和 JSLint 报告空块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31605781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do TSLint and JSLint report empty blocks?
提问by theDmi
From time to time, I get TSLint errors "block is empty". This happens e.g. when I pass a no-op callback to a function:
有时,我会收到 TSLint 错误“块为空”。例如,当我将无操作回调传递给函数时,就会发生这种情况:
doSomething(() => {});
From what I read, JSLint apparently does the same, but I didn't verify that.
从我读到的,JSLint 显然做了同样的事情,但我没有验证。
I find these usages completely valid, so I tried to find a reason why empty blocks are considered bad at all. But the only thing I'm able to find (e.g. in this answer) are instructions to add a return;
to avoid the error. This is notwhat I want to do in every empty callback.
我发现这些用法完全有效,所以我试图找到一个原因,为什么空块被认为是不好的。但是我唯一能找到的东西(例如在这个答案中)是添加 areturn;
以避免错误的说明。这不是我想在每个空回调中做的事情。
Why does TSLint report above empty block as problem? Is there any reason why I shouldn't disable the check?
为什么 TSLint 将上面的空块报告为问题?有什么理由我不应该禁用检查吗?
回答by basarat
Why does TSLint report above empty block as problem
为什么 TSLint 将上面的空块报告为问题
To prevent mistakes. Perhaps the function was forgotten to be filled out. Recommend () => undefined
as a noop.
防止出错。可能是函数忘记填写了。推荐() => undefined
作为 noop。
More
更多的
If you want to disable it simply add "no-empty": false,
to your tslint.json
(globally disable) or disable it inline using a /* tslint:disable:no-empty */
comment.
如果您想禁用它,只需添加"no-empty": false,
到您的tslint.json
(全局禁用)或使用/* tslint:disable:no-empty */
注释内联禁用它。
回答by Fenton
As with all checks, you have the ultimate judgement on whether they are helping you or not. You can switch off this TSLint check using one of the following options.
与所有检查一样,您对它们是否帮助您有最终判断。您可以使用以下选项之一关闭此 TSLint 检查。
Disable the rule in tslint.json
禁用 tslint.json 中的规则
//...
"no-empty": false,
//...
Disable the rule in the file:
禁用文件中的规则:
/* tslint:disable:no-empty */
You can always switch it back on again if sometime in the future you find an empty block that has caused you a problem.
如果将来某个时候您发现一个导致您出现问题的空块,您可以随时将其重新打开。
回答by Estus Flask
A way to suppress the error and designate that empty block was intentionally is to disable the rule temporary:
一种抑制错误并故意指定空块的方法是暂时禁用规则:
// tslint:disable-next-line:no-empty
doSomething(() => {});
Or make it non-empty:
或者让它非空:
doSomething(() => {/**/});
回答by Tomas Varga
tslint v5.10.0introduced "allow-empty-functions"
option to "no-empty"
just for this case;
also "allow-empty-catch"
(introduced in v5.5.0) may be useful:
tslint v5.10.0为这种情况引入了"allow-empty-functions"
选项"no-empty"
;
也"allow-empty-catch"
(在v5.5.0 中引入)可能有用:
"no-empty": [true, "allow-empty-functions", "allow-empty-catch"]
回答by spacedev
If you feel you dont want to use callback during certain scenarios you can modify the code
如果您觉得在某些情况下不想使用回调,您可以修改代码
from
从
doSomething(() => {});
to
到
doSomething(() => undefined);
Substituting () => {} to this implies you dont care about this callback. and explicit typecasting will avoid implications.
将 () => {} 替换为 this 意味着您不关心此回调。显式类型转换将避免影响。
Good luck.
祝你好运。