You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refine guidance on leveraging TypeScript types for explicit boolean checks.
Clarify that redundant checks for conditions already guaranteed by type
definitions (e.g., null checks for `SomeType \| undefined`, or undefined
checks for non-optional members) should be omitted.
Signed-off-by: Eric Wheeler <[email protected]>
Copy file name to clipboardExpand all lines: .roo/rules/eslint-no-implicit-coercion.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,11 @@ When ESLint's `no-implicit-coercion` rule flags an error, a value is being impli
6
6
7
7
For boolean coercions (e.g., in `if` statements or ternaries), MUST NOT use `Boolean(value)`. Instead, extend conditional expressions to explicitly compare against the specific data type and value expected for that implementation context.
8
8
9
+
- Leverage TypeScript type information to make these explicit comparisons precise. For example:
10
+
- Redundant checks for conditions already guaranteed by TypeScript type definitions (e.g., checking for `null` when a type is `SomeType | undefined`, or checking for `undefined` on a non-optional, initialized variable) MUST be omitted.
11
+
- If a string type is a union of specific non-empty literals (e.g., `"active" | "pending"`), `typeof variable === "string"` can be sufficient for its "truthiness" if all literals are inherently truthy.
12
+
- If types and initialization guarantee a variable is always defined and non-null (e.g., a non-optional class member), runtime checks for its mere existence can be redundant; the explicit boolean should reflect this guarantee.
13
+
9
14
YOU MUST NEVER replace `if (myVar)` with `if (Boolean(myVar))` or `if (!!myVar)` with `if (Boolean(myVar))`.
10
15
11
16
This rule's purpose is to encourage a thoughtful evaluation of what "truthy" or "falsy" means for the specific variable and logic.
@@ -75,5 +80,6 @@ Explicitly comparing types and values:
0 commit comments