ESLint错误不需要 - 三元组 [英] ESLint error no-unneeded-ternary
问题描述
ESLint在我的JS模块中告诉我这条错误消息: 错误no-unneeded-ternary默认分配不必要地使用条件表达式
ESLint is telling me this error message inside my JS module: error no-unneeded-ternary Unnecessary use of conditional expression for default assignment
错误来自返回
语句上的
? get
方法>返回val? val:defaultVal;
The error comes in the get
method on the return
statement return val ? val : defaultVal;
?
import ls from 'local-storage';
export default {
get(key, defaultVal = null) {
var val = ls(key);
return val ? val : defaultVal;
},
set(key, val) {
return ls(key, val);
},
remove(key) {
return ls.remove(key);
},
};
我知道为什么会收到此错误消息?我在ESLint的网站上找到了一些有关此错误消息的资源此处,但它适用于布尔表达式,我无法弄清楚为什么这适用于我的代码...
Any idea why do I get this error message? I have found some resource on ESLint's website regarding this error message here but it applies to boolean expressions and I can not figure out why would that apply to my code...
推荐答案
你不需要三元组当一个简单的 val ||时defaultVal
将会这样做。
You don't need a ternary when a simple val || defaultVal
will do.
这篇关于ESLint错误不需要 - 三元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!