type a = [1,2,3]
type Invert<T extends any[] & {'0': any}> = ???
type b = Invert<a> // should yield [3,2,1]
I am stucked to figure out the definition of Invert
type of a tuple,
also an Init
and Last
type, although they may be constructed of each others
what I have tried:
Rest
part, this approach only got the Tail
part with rest paramsas of typescript 4.0 code to approach a Reverse type is much easier than before, saying
type Reverse<T extends any[], R extends any[] = []> = ReturnType<T extends [infer F, ...infer L] ? () => Reverse<L,[F,...R]> : () => R>
some explanation:
Reverse
in typealias Reverse
will result in circularly references error.Reverse
type in function type (()=> Reverse
) will opt-out the circularly references errorReturnType<() => Tup<L,[F,...R]>>
wont do the trick, but
ReturnType<ConditionalType>
will do