Skip to main content

NonEmptyArray<T>

NonEmptyArray<T> = [T, ...T[]]

An array type that guarantees at least one element.

Useful for functions that require non-empty input, avoiding runtime checks.


Type Parametersโ€‹

T: Tโ€‹

The type of array elements


Sinceโ€‹

2.0.0


Exampleโ€‹

function first`<T>`(arr: `NonEmptyArray<T>`): T {
return arr[0]; // โœ… Safe, TypeScript knows arr[0] exists
}

first([1, 2, 3]); // โœ… OK
first([]); // โŒ Error: Source has 0 element(s) but target requires 1

const items: `NonEmptyArray<string>` = ["a", "b"];
items[0]; // Type is `string`, not `string | undefined`