跳到主要内容

Flatten

介绍

在这个挑战中,你需要写一个接受数组的类型,并且返回扁平化的数组类型。

ts
type flatten = Flatten<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, 5]
ts
type flatten = Flatten<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, 5]
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type Flatten = any
 
/* _____________ Test Cases _____________ */
 
 
 
type cases = [
Expect<Equal<Flatten<[]>, []>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
Expect<Equal<Flatten<[1, 2, 3, 4]>, [1, 2, 3, 4]>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
Expect<Equal<Flatten<[1, [2]]>, [1, 2]>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
Expect<Equal<Flatten<[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, 5]>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
Expect<Equal<Flatten<[{ foo: 'bar', 2: 10 }, 'foobar']>, [{ foo: 'bar', 2: 10 }, 'foobar']>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type Flatten = any
 
/* _____________ Test Cases _____________ */
 
 
 
type cases = [
Expect<Equal<Flatten<[]>, []>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
Expect<Equal<Flatten<[1, 2, 3, 4]>, [1, 2, 3, 4]>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
Expect<Equal<Flatten<[1, [2]]>, [1, 2]>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
Expect<Equal<Flatten<[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, 5]>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
Expect<Equal<Flatten<[{ foo: 'bar', 2: 10 }, 'foobar']>, [{ foo: 'bar', 2: 10 }, 'foobar']>>,
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
2344
2315
Type 'false' does not satisfy the constraint 'true'.
Type 'Flatten' is not generic.
]
 
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
type Flatten<T extends any[]> = T extends [infer F, ...infer Rest]
? F extends any[]
? [...Flatten<F>, ...Flatten<Rest>]
: [F, ...Flatten<Rest>]
: T;
ts
type Flatten<T extends any[]> = T extends [infer F, ...infer Rest]
? F extends any[]
? [...Flatten<F>, ...Flatten<Rest>]
: [F, ...Flatten<Rest>]
: T;
ts
// most popular
 
type Flatten<S extends any[], T extends any[] = []> = S extends [infer X, ...infer Y] ?
X extends any[] ?
Flatten<[...X, ...Y], T> : Flatten<[...Y], [...T, X]>
: T;
 
 
ts
// most popular
 
type Flatten<S extends any[], T extends any[] = []> = S extends [infer X, ...infer Y] ?
X extends any[] ?
Flatten<[...X, ...Y], T> : Flatten<[...Y], [...T, X]>
: T;
 
 
view more solutions