Skip to content

Type Alias: Last<ValueType>

ts
type Last<ValueType> = ValueType extends readonly [infer ElementType]
  ? ElementType
  : ValueType extends readonly [infer _, ...infer Tail]
    ? Last<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

Type Parameter
ValueType extends readonly unknown[]

Author

sindresorhus https://github.com/sindresorhus/type-fest