跳到主要内容

FlattenDepth

介绍

递归摊平数组. 如果提供了深度, 应确保它是正数.

ts
type a = FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2> // [1, 2, 3, 4, [5]]. flattern 2 times
type b = FlattenDepth<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, [[5]]]. Depth defaults to be 1
ts
type a = FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2> // [1, 2, 3, 4, [5]]. flattern 2 times
type b = FlattenDepth<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, [[5]]]. Depth defaults to be 1
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type FlattenDepth<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<FlattenDepth<[]>, []>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<FlattenDepth<[1, 2, 3, 4]>, [1, 2, 3, 4]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<FlattenDepth<[1, [2]]>, [1, 2]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2>, [1, 2, 3, 4, [5]]>>,
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
2344
2314
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
Expect<Equal<FlattenDepth<[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, [[5]]]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<FlattenDepth<[1, [2, [3, [4, [5]]]]], 3>, [1, 2, 3, 4, [5]]>>,
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
2344
2314
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
Expect<Equal<FlattenDepth<[1, [2, [3, [4, [5]]]]], 19260817>, [1, 2, 3, 4, 5]>>,
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
2344
2314
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
]
ts
/* _____________ Your Code Here _____________ */
 
type FlattenDepth<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<FlattenDepth<[]>, []>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<FlattenDepth<[1, 2, 3, 4]>, [1, 2, 3, 4]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<FlattenDepth<[1, [2]]>, [1, 2]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2>, [1, 2, 3, 4, [5]]>>,
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
2344
2314
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
Expect<Equal<FlattenDepth<[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, [[5]]]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<FlattenDepth<[1, [2, [3, [4, [5]]]]], 3>, [1, 2, 3, 4, [5]]>>,
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
2344
2314
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
Expect<Equal<FlattenDepth<[1, [2, [3, [4, [5]]]]], 19260817>, [1, 2, 3, 4, 5]>>,
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
2344
2314
Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).
]
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
// most popular
 
type FlattenDepth<
T extends any[],
S extends number = 1,
U extends any[] = []
> = U['length'] extends S
? T
: T extends [infer F, ...infer R]
? F extends any[]
? [...FlattenDepth<F, S, [...U, 1]>, ...FlattenDepth<R, S, U>]
: [F, ...FlattenDepth<R, S, U>]
: T
 
ts
// most popular
 
type FlattenDepth<
T extends any[],
S extends number = 1,
U extends any[] = []
> = U['length'] extends S
? T
: T extends [infer F, ...infer R]
? F extends any[]
? [...FlattenDepth<F, S, [...U, 1]>, ...FlattenDepth<R, S, U>]
: [F, ...FlattenDepth<R, S, U>]
: T
 
ts
// my solution
 
/**
* 存在的问题: NumToArr由于19260817存在, 递归次数超过限制,产生ts报错. 所以当时硬编码了50次递归
*/
 
type NumToArr<N extends number = 1, U extends any[] = []> = N extends U['length']
? U
: U['length'] extends 50
? U
: NumToArr<N, [...U, N]>;
 
type NumMinusOne<T extends any[]> = T['length'] extends 1
? 0
: T extends [infer F, ...infer Rest]
? Rest['length']
: 0;
 
type desct<T extends any[], U extends any[] = []> = T extends [infer F, ...infer Rest]
? desct<Rest, [
...U,
...(F extends any[] ? F : [F])
]>
: U;
 
 
type FlattenDepth<T extends any[], D extends number = 1, U extends any[] = T> = D extends 0
? U
: FlattenDepth<desct<U>, NumMinusOne<NumToArr<D, []>>>;
 
ts
// my solution
 
/**
* 存在的问题: NumToArr由于19260817存在, 递归次数超过限制,产生ts报错. 所以当时硬编码了50次递归
*/
 
type NumToArr<N extends number = 1, U extends any[] = []> = N extends U['length']
? U
: U['length'] extends 50
? U
: NumToArr<N, [...U, N]>;
 
type NumMinusOne<T extends any[]> = T['length'] extends 1
? 0
: T extends [infer F, ...infer Rest]
? Rest['length']
: 0;
 
type desct<T extends any[], U extends any[] = []> = T extends [infer F, ...infer Rest]
? desct<Rest, [
...U,
...(F extends any[] ? F : [F])
]>
: U;
 
 
type FlattenDepth<T extends any[], D extends number = 1, U extends any[] = T> = D extends 0
? U
: FlattenDepth<desct<U>, NumMinusOne<NumToArr<D, []>>>;
 
view more solutions