ESLint错误不需要 - 三元组 [英] ESLint error no-unneeded-ternary

查看:1522
本文介绍了ESLint错误不需要 - 三元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆