sizeComparisonOperators
Enforce consistent style for
.lengthand.sizechecks.
✅ This rule is included in the ts stylisticStrict presets.
This rule enforces a consistent style for checking .length and .size properties in boolean contexts.
By default, the rule prefers implicit boolean coercion (e.g., if (array.length)) for its conciseness.
Alternatively, you can configure it to prefer explicit comparisons (e.g., if (array.length > 0)) for clarity.
Examples
Section titled “Examples”Default (style: "coercion")
Section titled “Default (style: "coercion")”declare const items: string[];if (items.length > 0) {}declare const items: string[];if (items.length === 0) {}declare const items: string[];if (items.length !== 0) {}declare const mySet: Set<string>;if (mySet.size > 0) {}declare const items: string[];if (items.length) {}declare const items: string[];if (!items.length) {}declare const items: string[];items.length && doSomething();declare const items: string[];const hasItems = items.length ? "yes" : "no";declare const mySet: Set<string>;if (mySet.size) {}With style: "explicit"
Section titled “With style: "explicit"”declare const items: string[];if (items.length) {}declare const items: string[];if (!items.length) {}declare const items: string[];items.length && doSomething();declare const items: string[];const hasItems = items.length ? "yes" : "no";declare const items: string[];Boolean(items.length);declare const mySet: Set<string>;if (mySet.size) {}declare const items: string[];if (items.length > 0) {}declare const items: string[];if (items.length === 0) {}declare const items: string[];if (items.length !== 0) {}declare const items: string[];const count = items.length;declare const items: string[];const count = items.length ?? 0;declare const items: string[];const value = items.length || 1;declare const mySet: Set<string>;if (mySet.size > 0) {}Options
Section titled “Options”- Type:
"coercion" | "explicit" - Default:
"coercion"
Which style to enforce:
"coercion": Prefer implicit boolean checks likeif (array.length)"explicit": Prefer explicit comparisons likeif (array.length > 0)
When Not To Use It
Section titled “When Not To Use It”If your team doesn’t have a preference for one style over the other, or if you want to allow both styles, you might prefer to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useExplicitLengthCheck - ESLint:
unicorn/explicit-length-check - Oxlint:
unicorn/explicit-length-check
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.