Function initial

  • Returns an empty array when the input is a tuple containing exactly one element.

    Type Parameters

    • T

      The type of the single element.

    Parameters

    • arr: readonly [T]

      A tuple containing exactly one element.

    Returns []

    An empty array since there is only one element.

    const array = [100] as const;
    const result = initial(array);
    // result will be []
  • Returns an empty array when the input array is empty.

    Parameters

    • arr: readonly []

    Returns []

    Always returns an empty array for an empty input.

    const array = [] as const;
    const result = initial(array);
    // result will be []
  • Returns a new array containing all elements except the last one from a tuple with multiple elements.

    Type Parameters

    • T

      The types of the initial elements.

    • U

      The type of the last element in the tuple.

    Parameters

    • arr: readonly [T, U]

      A tuple with one or more elements.

    Returns T[]

    A new array containing all but the last element of the tuple.

    const array = ['apple', 'banana', 'cherry'] as const;
    const result = initial(array);
    // result will be ['apple', 'banana']
  • Returns a new array containing all elements except the last one from the input array. If the input array is empty or has only one element, the function returns an empty array.

    Type Parameters

    • T

      The type of elements in the array.

    Parameters

    • arr: readonly T[]

      The input array.

    Returns T[]

    A new array containing all but the last element of the input array.

    const arr = [1, 2, 3, 4];
    const result = initial(arr);
    // result will be [1, 2, 3]