Reverse
介绍
实现类型版本的数组反转 Array.reverse
ts
type a = Reverse<['a', 'b']> // ['b', 'a']type b = Reverse<['a', 'b', 'c']> // ['c', 'b', 'a']
View on GitHubts
type a = Reverse<['a', 'b']> // ['b', 'a']type b = Reverse<['a', 'b', 'c']> // ['c', 'b', 'a']
起点
ts
/* _____________ Your Code Here _____________ */typeReverse <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Reverse <[]>, []>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Reverse <['a', 'b']>, ['b', 'a']>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Reverse <['a', 'b', 'c']>, ['c', 'b', 'a']>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeReverse <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Reverse <[]>, []>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Reverse <['a', 'b']>, ['b', 'a']>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Reverse <['a', 'b', 'c']>, ['c', 'b', 'a']>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeReverse <T extends any[]> =T extends [inferF , ...inferRest ]? [...Reverse <Rest >,F ]: []
ts
typeReverse <T extends any[]> =T extends [inferF , ...inferRest ]? [...Reverse <Rest >,F ]: []