First of Array
Implement a generic First<T> that takes an Array T and returns its first element's type.
For example:
tstypearr1 = ['a', 'b', 'c']typearr2 = [3, 2, 1]typeCannot find name 'First'.2304Cannot find name 'First'.head1 =< First arr1 > // expected to be 'a'typeCannot find name 'First'.2304Cannot find name 'First'.head2 =< First arr2 > // expected to be 3
tstypearr1 = ['a', 'b', 'c']typearr2 = [3, 2, 1]typeCannot find name 'First'.2304Cannot find name 'First'.head1 => // expected to be 'a' First <arr1 typeCannot find name 'First'.2304Cannot find name 'First'.head2 => // expected to be 3 First <arr2
Solution ✅
tstypearr1 = ['a', 'b', 'c']typearr2 = [3, 2, 1]typearr3 = []typeFirst <A > =A extends [inferFirst , ...any[]] ?First : nevertypehead1 =First <arr1 > // expected to be 'a'typehead2 =First <arr2 > // expected to be 3typehead3 =First <arr3 > // expected to be never
tstypearr1 = ['a', 'b', 'c']typearr2 = [3, 2, 1]typearr3 = []typeFirst <A > =A extends [inferFirst , ...any[]] ?First : nevertypehead1 =First <arr1 > // expected to be 'a'typehead2 =First <arr2 > // expected to be 3typehead3 =First <arr3 > // expected to be never