prefer-ts-extras-has-at-least
Prefer hasAtLeast when checking whether an array contains a known minimum number of elements.
Targeted pattern scopeโ
The rule accepts literal minima from 2 through 999, including reversed comparisons and equivalent strict bounds. For example, 1 < items.length and items.length >= 2 both suggest hasAtLeast(items, 2). The published overload only narrows when the minimum is one integer literal below 1000.
What this rule reportsโ
A numeric comparison of a direct .length access on a mutable array, readonly array, tuple, or a union of those types. Strings, typed arrays, arbitrary objects with a length property, optional access and computed properties are outside the rule's scope.
Why this rule existsโ
The helper narrows the array so TypeScript can safely read the required leading elements with noUncheckedIndexedAccess enabled. An optional tuple element or an element whose declared type includes undefined can still be undefined.
โ Incorrectโ
declare const items: readonly string[];
if (items.length >= 2) {
console.log(items[1]);
}
โ Correctโ
import { hasAtLeast } from "ts-extras";
declare const items: readonly string[];
if (hasAtLeast(items, 2)) {
console.log(items[1]);
}
Behavior and migration notesโ
The diagnostic requires type information and a stable ts-extras 2.x or 3.x release. The diagnostic leaves the edit to the developer. The helper changes TypeScript control-flow narrowing, including the type available in the false branch. The rule skips fixed tuple lengths that already determine the result.
When not to use itโ
Disable this rule when your team prefers explicit length comparisons.
Package documentationโ
ts-extras package documentation:
Rule catalog ID: R131