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

@returnsthe number of (unique) elements in Set.

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: () => void
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

@returnsthe number of (unique) elements in Set.

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 ❤️‍🔥 around the world by the Flint team and contributors.