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 const items: string[]
items: string[];if (const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length > 0) {}declare const const items: string[]
items: string[];if (const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length === 0) {}declare const const items: string[]
items: string[];if (const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length !== 0) {}declare const const mySet: Set<string>
mySet: interface Set<T>
Set<string>;if (const mySet: Set<string>
mySet.Set<string>.size: number
size > 0) {}declare const const items: string[]
items: string[];if (const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length) {}declare const const items: string[]
items: string[];if (!const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length) {}declare const const items: string[]
items: string[];const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length && const doSomething: any
doSomething();declare const const items: string[]
items: string[];const const hasItems: "yes" | "no"
hasItems = const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length ? "yes" : "no";declare const const mySet: Set<string>
mySet: interface Set<T>
Set<string>;if (const mySet: Set<string>
mySet.Set<string>.size: number
size) {}With style: "explicit"
Section titled “With style: "explicit"”declare const const items: string[]
items: string[];if (const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length) {}declare const const items: string[]
items: string[];if (!const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length) {}declare const const items: string[]
items: string[];const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length && const doSomething: any
doSomething();declare const const items: string[]
items: string[];const const hasItems: "yes" | "no"
hasItems = const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length ? "yes" : "no";declare const const items: string[]
items: string[];var Boolean: BooleanConstructor<number>(value?: number | undefined) => boolean
Boolean(const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length);declare const const mySet: Set<string>
mySet: interface Set<T>
Set<string>;if (const mySet: Set<string>
mySet.Set<string>.size: number
size) {}declare const const items: string[]
items: string[];if (const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length > 0) {}declare const const items: string[]
items: string[];if (const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length === 0) {}declare const const items: string[]
items: string[];if (const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length !== 0) {}declare const const items: string[]
items: string[];const const count: number
count = const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length;declare const const items: string[]
items: string[];const const count: number
count = const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length ?? 0;declare const const items: string[]
items: string[];const const value: number
value = const items: string[]
items.Array<string>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length || 1;declare const const mySet: Set<string>
mySet: interface Set<T>
Set<string>;if (const mySet: Set<string>
mySet.Set<string>.size: number
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