简体   繁体   中英

Is there an ESLint rule to prevent truthy checks

I want to avoid accidentally invoking Javascript's insane truthy system. Are there any ESLint rules to help with this? Especially in if statements. 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.

Not sure if there is a rule that will do that, but if there is it will need to be a typescript-eslint rule. Javascript alone doesn't have enough information to statically determine if coercion will happen. You can see the typescript-eslint rules here: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/src/rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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