15 - Last of Array
Author: VarunMendreCreated Sep 17, 2026Updated Sep 17, 2026
Labelsansweren15
// Extracts the last element by splitting the tuple into:
// Rest = all elements except last
// Last = final element
type Last<T extends unknown[]> =
T extends [...infer Rest, infer Last]
? Last
: never;
// Adds a dummy `never` at the beginning,
// then uses the original tuple length as an index.
// That shifted index points to the last element.
type Last<T extends unknown[]> =
[never, ...T][T["length"]];
Source: type-challenges/type-challenges