API reference@evolu/commonArray › isNonEmptyArray

Call Signature

function isNonEmptyArray<T>(array: T[]): array is [T, ...T[]];

Defined in: packages/common/src/Array.ts:255

Checks if an array is non-empty and narrows its type to NonEmptyArray or NonEmptyReadonlyArray based on the input.

To check if an array is empty, use if (!isNonEmptyArray(arr)) — using the negated guard is better than .length === 0 for early returns because TypeScript narrows the type after the check.

Example

// Mutable array narrows to NonEmptyArray
const arr: Array<number> = [1, 2, 3];
if (isNonEmptyArray(arr)) {
  shiftFromArray(arr); // arr is NonEmptyArray<number>
}

// Readonly array narrows to NonEmptyReadonlyArray
const readonly: ReadonlyArray<number> = [1, 2, 3];
if (isNonEmptyArray(readonly)) {
  firstInArray(readonly); // readonly is NonEmptyReadonlyArray<number>
}

Call Signature

function isNonEmptyArray<T>(array: readonly T[]): array is readonly [T, T];

Defined in: packages/common/src/Array.ts:257

Readonly array overload.