简体   繁体   English

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

[英]Is there an ESLint rule to prevent truthy checks

I want to avoid accidentally invoking Javascript's insane truthy system.我想避免意外调用 Javascript 的疯狂真实系统。 Are there any ESLint rules to help with this?是否有任何 ESLint 规则可以帮助解决这个问题? Especially in if statements.特别是在if语句中。 For example:例如:

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

I thought no-implicit-coercion might do the job but it seems like it doesn't cover this case.我认为no-implicit-coercion可能会完成这项工作,但它似乎不包括这种情况。

Not sure if there is a rule that will do that, but if there is it will need to be a typescript-eslint rule.不确定是否有规则可以做到这一点,但如果有,则需要是 typescript-eslint 规则。 Javascript alone doesn't have enough information to statically determine if coercion will happen.单独的 Javascript 没有足够的信息来静态确定是否会发生强制转换。 You can see the typescript-eslint rules here: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/src/rules你可以在这里看到 typescript-eslint 规则: https : //github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/src/rules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM