Skip to content

sizeComparisonOperators

Enforce consistent style for .length and .size checks.

✅ 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.

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 items: string[];
Boolean(items.length);
declare const mySet: Set<string>;
if (mySet.size) {
}
  • Type: "coercion" | "explicit"
  • Default: "coercion"

Which style to enforce:

  • "coercion": Prefer implicit boolean checks like if (array.length)
  • "explicit": Prefer explicit comparisons like if (array.length > 0)

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.