Skip to main content

prefer-ts-extras-is-empty

Require isEmpty from ts-extras over direct array.length === 0 checks.

Targeted pattern scopeโ€‹

This rule only matches strict zero-length comparisons against .length that can be rewritten as isEmpty(array).

  • Direct empty-array checks using length equality:
  • array.length === 0
  • 0 === array.length

Syntactically similar alternatives are intentionally out of scope unless they preserve the same AST shape.

What this rule reportsโ€‹

This rule reports strict empty-check comparisons that can be replaced with isEmpty(array).

  • Direct empty-array checks using length equality:
  • array.length === 0
  • 0 === array.length

Why this rule existsโ€‹

isEmpty gives one canonical emptiness predicate instead of repeated length comparisons.

  • Emptiness checks are easier to search and standardize.
  • Predicate style is consistent with other ts-extras guards.
  • Repeated comparison variants are removed from call sites.

โŒ Incorrectโ€‹

if (items.length === 0) {
return;
}

โœ… Correctโ€‹

if (isEmpty(items)) {
return;
}

Behavior and migration notesโ€‹

  • isEmpty(array) is equivalent to array.length === 0.
  • Use !isEmpty(array) for non-empty checks.
  • This rule is about array emptiness checks, not object key-count checks.

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

if (items.length === 0) {
return;
}

โœ… Correct โ€” Additional exampleโ€‹

if (isEmpty(items)) {
return;
}

โœ… Correct โ€” Repository-wide usageโ€‹

const hasRows = !isEmpty(rows);

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-ts-extras-is-empty": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your team requires direct .length comparisons for emptiness checks.

Package documentationโ€‹

ts-extras package documentation:

Source file: source/is-empty.ts

/**
Check whether an array is empty.

This is useful because doing `array.length === 0` on its own won't work as a type-guard.

@example
```
import {isEmpty} from 'ts-extras';

isEmpty([1, 2, 3]);
//=> false

isEmpty([]);
//=> true

// Works with tuples
const tuple: [string, number] | [] = Math.random() > 0.5 ? ['hello', 42] : [];
if (isEmpty(tuple)) {
// tuple is now typed as []
} else {
// tuple is now typed as [string, number]
}
```

@category Type guard
*/

Rule catalog ID: R016

Further readingโ€‹

Adoption resourcesโ€‹