是否有 ESLint 规则来防止真实检查

Tim*_*mmm 5 typescript eslint typescript-eslint

我想避免意外调用 Javascript 的疯狂真实系统。是否有任何 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
Run Code Online (Sandbox Code Playgroud)

我认为no-implicit-coercion可能会完成这项工作,但似乎不包括这种情况。

Nic*_*wer 3

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

  • 啊哈,找到了,谢谢![严格布尔表达式](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/strict-boolean-expressions.md) (3认同)