Skip to main content

Last of Array

Implement a generic Last<T> that takes an Array T and returns its last element.

For example:

ts
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
 
type tail1 = Last<arr1> // expected to be 'c'
Cannot find name 'Last'.2304Cannot find name 'Last'.
type tail2 = Last<arr2> // expected to be 1
Cannot find name 'Last'.2304Cannot find name 'Last'.
ts
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
 
type tail1 = Last<arr1> // expected to be 'c'
Cannot find name 'Last'.2304Cannot find name 'Last'.
type tail2 = Last<arr2> // expected to be 1
Cannot find name 'Last'.2304Cannot find name 'Last'.
Solution ✅
ts
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
 
type Last<T> = T extends [...any, infer K] ? K : never
 
type tail1 = Last<arr1> // expected to be 'c'
type tail2 = Last<arr2> // expected to be 1
type tail3 = Last<[0]> // expected to be 0
type tail4 = Last<[]> // expected to be never
ts
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
 
type Last<T> = T extends [...any, infer K] ? K : never
 
type tail1 = Last<arr1> // expected to be 'c'
type tail2 = Last<arr2> // expected to be 1
type tail3 = Last<[0]> // expected to be 0
type tail4 = Last<[]> // expected to be never