【问题标题】:Is there an ESLint rule to prevent truthy checks是否有 ESLint 规则来防止真实检查
【发布时间】:2020-06-05 03:08:48
【问题描述】:

我想避免意外调用 Javascript 的疯狂truthy 系统。是否有任何 ESLint 规则可以帮助解决这个问题?特别是在if 语句中。例如:

const a: number = 0;
const b: string | null = null;
if (a) { ... } // Should be an error.
if (b) { ... } // Should be an error.
if (a !== 0) { ... } // Ok
if (b !== null) { ... } // Ok

我认为no-implicit-coercion 可能会完成这项工作,但它似乎没有涵盖这种情况。

【问题讨论】:

    标签: typescript eslint typescript-eslint


    【解决方案1】:

    不确定是否有规则可以做到这一点,但如果有,则需要一个 typescript-eslint 规则。仅 Javascript 没有足够的信息来静态确定是否会发生强制。你可以在这里查看 typescript-eslint 规则:https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/src/rules

    【讨论】:

    猜你喜欢
    • 2016-05-15
    • 2019-06-16
    • 2021-12-21
    • 2019-10-20
    • 1970-01-01
    • 2020-03-09
    • 2020-05-25
    • 2021-03-07
    • 1970-01-01
    相关资源
    最近更新 更多