Type Alias LastArrayElement<ValueType>

LastArrayElement: ValueType extends readonly [infer ElementType]
    ? ElementType
    : ValueType extends readonly [infer _, ...(infer Tail)]
        ? LastArrayElement<Tail>
        : ValueType extends ReadonlyArray<infer ElementType> ? ElementType : never

Extracts the type of the last element of an array.

Use-case: Defining the return type of functions that extract the last element of an array, for example lodash.last.

Type Parameters

  • ValueType extends readonly unknown[]
import type {LastArrayElement} from 'type-fest';

declare function lastOf<V extends readonly any[]>(array: V): LastArrayElement<V>;

const array = ['foo', 2];

typeof lastOf(array);
//=> number