Skip to content

NonEmptyArray type ​

An array with at least one item, so that its first item is never undefined.

ts
import type { NonEmptyArray } from 'utils';

Signature ​

ts
export type NonEmptyArray<T> = readonly [T, ...T[]];

Parameters ​

NameDescription
TThe item type.

Example ​

ts
function average(values: NonEmptyArray<number>): number;

Tests ​

Line coverage: 0 %.

  • ✅ requires a first item

Source ​

src/types/non-empty-array.ts (on GitHub)

Used by ​

  • isNonEmptyArray: Checks whether an array has an item, and narrows it so that its first item is defined.
Source code
ts
/**
 * An array with at least one item, so that its first item is never `undefined`.
 *
 * @template T - The item type.
 * @example
 * function average(values: NonEmptyArray<number>): number;
 */
export type NonEmptyArray<T> = readonly [T, ...T[]];
Test file
ts
import type { NonEmptyArray } from './non-empty-array';

describe('NonEmptyArray', () => {
  it('requires a first item', () => {
    expectTypeOf<[1]>().toExtend<NonEmptyArray<number>>();
    expectTypeOf<[]>().not.toExtend<NonEmptyArray<number>>();
    expectTypeOf<NonEmptyArray<number>[0]>().toEqualTypeOf<number>();
  });
});

Keywords: non, empty, array, types, type

Generated from the JSDoc of the sources.