Last of Array
Implement a generic Last<T> that takes an Array T and returns its last element.
For example:
tstypearr1 = ['a', 'b', 'c']typearr2 = [3, 2, 1]typeCannot find name 'Last'.2304Cannot find name 'Last'.tail1 =< Last arr1 > // expected to be 'c'typeCannot find name 'Last'.2304Cannot find name 'Last'.tail2 =< Last arr2 > // expected to be 1
tstypearr1 = ['a', 'b', 'c']typearr2 = [3, 2, 1]typeCannot find name 'Last'.2304Cannot find name 'Last'.tail1 => // expected to be 'c' Last <arr1 typeCannot find name 'Last'.2304Cannot find name 'Last'.tail2 => // expected to be 1 Last <arr2
Solution ✅
tstypearr1 = ['a', 'b', 'c']typearr2 = [3, 2, 1]typeLast <T > =T extends [...any, inferK ] ?K : nevertypetail1 =Last <arr1 > // expected to be 'c'typetail2 =Last <arr2 > // expected to be 1typetail3 =Last <[0]> // expected to be 0typetail4 =Last <[]> // expected to be never
tstypearr1 = ['a', 'b', 'c']typearr2 = [3, 2, 1]typeLast <T > =T extends [...any, inferK ] ?K : nevertypetail1 =Last <arr1 > // expected to be 'c'typetail2 =Last <arr2 > // expected to be 1typetail3 =Last <[0]> // expected to be 0typetail4 =Last <[]> // expected to be never