Flatten
介绍
在这个挑战中,你需要写一个接受数组的类型,并且返回扁平化的数组类型。
ts
type flatten = Flatten<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, 5]
View on GitHubts
type flatten = Flatten<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, 5]
起点
ts
/* _____________ Your Code Here _____________ */typeFlatten = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[]>, []>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[1, 2, 3, 4]>, [1, 2, 3, 4]>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[1, [2]]>, [1, 2]>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, 5]>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[{foo : 'bar', 2: 10 }, 'foobar']>, [{foo : 'bar', 2: 10 }, 'foobar']>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.]
take the challengets
/* _____________ Your Code Here _____________ */typeFlatten = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[]>, []>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[1, 2, 3, 4]>, [1, 2, 3, 4]>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[1, [2]]>, [1, 2]>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, 5]>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Flatten <[{foo : 'bar', 2: 10 }, 'foobar']>, [{foo : 'bar', 2: 10 }, 'foobar']>>,
Type 'Flatten' is not generic.2344
2315Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.]
解决方案
Spoiler warning // Click to reveal answer
ts
typeFlatten <T extends any[]> =T extends [inferF , ...inferRest ]?F extends any[]? [...Flatten <F >, ...Flatten <Rest >]: [F , ...Flatten <Rest >]:T ;
ts
typeFlatten <T extends any[]> =T extends [inferF , ...inferRest ]?F extends any[]? [...Flatten <F >, ...Flatten <Rest >]: [F , ...Flatten <Rest >]:T ;
ts
// most populartypeFlatten <S extends any[],T extends any[] = []> =S extends [inferX , ...inferY ] ?X extends any[] ?Flatten <[...X , ...Y ],T > :Flatten <[...Y ], [...T ,X ]>:T ;
ts
// most populartypeFlatten <S extends any[],T extends any[] = []> =S extends [inferX , ...inferY ] ?X extends any[] ?Flatten <[...X , ...Y ],T > :Flatten <[...Y ], [...T ,X ]>:T ;