ts-data-forge
    Preparing search index...
    • Casts an array to FixedLengthTuple<Length, E> after checking that it has exactly length elements.

      This is the structural counterpart of asFixedLengthArray: it produces a tuple type rather than the branded FixedLengthArray. Prefer the branded one unless you specifically need the structural type — the brand keeps type-checking cost independent of the element type and of the bound.

      The length argument comes first, matching the type-parameter order of FixedLengthTuple<Length, Elm>. Omitting the array returns a function that casts one.

      Type Parameters

      • Length extends ArgArr

        The exact number of elements.

      • Xs extends readonly unknown[]

        The input array type (tuple types are preserved).

      Parameters

      • length: Length

        The exact number of elements.

      • xs: Xs

        The array to cast.

      Returns FixedLengthTuple<Length, Xs[number]> & Xs

      xs typed as FixedLengthTuple<Length, Xs[number]> & Xs.

      const rgb = Arr.asFixedLengthTuple(3, [255, 128, 0]);

      const red: number = rgb[0]; // OK — no `undefined`

      // curried version
      const asRgb = Arr.asFixedLengthTuple(3);

      // Arr.asFixedLengthTuple(3, [255, 128]); // throws TypeError

      If xs.length !== length.

    • Casts an array to FixedLengthTuple<Length, E> after checking that it has exactly length elements.

      This is the structural counterpart of asFixedLengthArray: it produces a tuple type rather than the branded FixedLengthArray. Prefer the branded one unless you specifically need the structural type — the brand keeps type-checking cost independent of the element type and of the bound.

      The length argument comes first, matching the type-parameter order of FixedLengthTuple<Length, Elm>. Omitting the array returns a function that casts one.

      Type Parameters

      • Length extends ArgArr

        The exact number of elements.

      Parameters

      • length: Length

        The exact number of elements.

      Returns <Xs extends readonly unknown[]>(
          xs: Xs,
      ) => FixedLengthTuple<Length, Xs[number]> & Xs

      xs typed as FixedLengthTuple<Length, Xs[number]> & Xs.

      const rgb = Arr.asFixedLengthTuple(3, [255, 128, 0]);

      const red: number = rgb[0]; // OK — no `undefined`

      // curried version
      const asRgb = Arr.asFixedLengthTuple(3);

      // Arr.asFixedLengthTuple(3, [255, 128]); // throws TypeError

      If xs.length !== length.