ts-type-forge
    Preparing search index...

    Type Alias Last<T>

    Last: T extends readonly []
        ? never
        : T extends readonly [...readonly unknown[], infer L] ? L : T[number]

    Gets the type of the last element of a readonly tuple T. If the tuple is empty, it returns never. For a general (non-tuple) array, it returns the element type. For a tuple with a rest element (e.g. [1, ...string[]]), the exact last element is not statically known, so the union of all element types is returned.

    Type Parameters

    • T extends readonly unknown[]

      The readonly tuple type.

    The type of the last element, or never if T is empty.

    type L1 = Tuple.Last<[1, 2, 3]>; // 3
    type L2 = Tuple.Last<[]>; // never
    type L3 = Tuple.Last<[1]>; // 1
    type L4 = Tuple.Last<readonly string[]>; // string
    type L5 = Tuple.Last<[...string[], 1]>; // 1
    type L6 = Tuple.Last<[1, ...string[]]>; // 1 | string