ts-data-forge
    Preparing search index...
    • Casts an array to NonEmptyArray<E> after checking that it has at least one element.

      NonEmptyArray<E> is an alias of MinLengthArray<1, E>, so this is the length-1 specialization of asMinLengthArray. Use this instead of writing xs as unknown as NonEmptyArray<E>: the length is verified at runtime, and the original array type (e.g. tuple types) is preserved via intersection, so the result also benefits from the structural tuple prefix (xs[0] without undefined under noUncheckedIndexedAccess).

      Type Parameters

      • Xs extends readonly unknown[]

        The input array type (tuple types are preserved).

      Parameters

      • xs: Xs

        The array to cast.

      Returns readonly [Xs[number], Xs[number]] & Readonly<{ MinLength: readonly [0, 0] }> & Readonly<
          {
              "TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3": unknown;
          },
      > & Xs

      xs typed as NonEmptyArray<Xs[number]> & Xs.

      const history = Arr.asNonEmptyArray([0, 1, 2, 3]);

      const first: number = history[0]; // OK — no `undefined`

      // Arr.asNonEmptyArray([]); // throws TypeError

      If xs is empty.