FlattenDepth
介绍
递归摊平数组. 如果提供了深度, 应确保它是正数.
ts
type a = FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2> // [1, 2, 3, 4, [5]]. flattern 2 timestype b = FlattenDepth<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, [[5]]]. Depth defaults to be 1
View on GitHubts
type a = FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2> // [1, 2, 3, 4, [5]]. flattern 2 timestype b = FlattenDepth<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, [[5]]]. Depth defaults to be 1
起点
ts
/* _____________ Your Code Here _____________ */typeFlattenDepth <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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'.Expect <Equal <FlattenDepth <[1, 2, [3, 4], [[[5]]]], 2>, [1, 2, 3, 4, [5]]>>,
Generic type 'FlattenDepth' requires 1 type argument(s).2344
2314Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <FlattenDepth <[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, [[5]]]>>,Type 'false' does not satisfy the constraint 'true'.Expect <Equal <FlattenDepth <[1, [2, [3, [4, [5]]]]], 3>, [1, 2, 3, 4, [5]]>>,
Generic type 'FlattenDepth' requires 1 type argument(s).2344
2314Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).Type 'false' does not satisfy the constraint 'true'.Expect <Equal <FlattenDepth <[1, [2, [3, [4, [5]]]]], 19260817>, [1, 2, 3, 4, 5]>>,
Generic type 'FlattenDepth' requires 1 type argument(s).2344
2314Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).]
take the challengets
/* _____________ Your Code Here _____________ */typeFlattenDepth <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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'.Expect <Equal <FlattenDepth <[1, 2, [3, 4], [[[5]]]], 2>, [1, 2, 3, 4, [5]]>>,
Generic type 'FlattenDepth' requires 1 type argument(s).2344
2314Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <FlattenDepth <[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, [[5]]]>>,Type 'false' does not satisfy the constraint 'true'.Expect <Equal <FlattenDepth <[1, [2, [3, [4, [5]]]]], 3>, [1, 2, 3, 4, [5]]>>,
Generic type 'FlattenDepth' requires 1 type argument(s).2344
2314Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).Type 'false' does not satisfy the constraint 'true'.Expect <Equal <FlattenDepth <[1, [2, [3, [4, [5]]]]], 19260817>, [1, 2, 3, 4, 5]>>,
Generic type 'FlattenDepth' requires 1 type argument(s).2344
2314Type 'false' does not satisfy the constraint 'true'.
Generic type 'FlattenDepth' requires 1 type argument(s).]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeFlattenDepth <T extends any[],S extends number = 1,U extends any[] = []> =U ['length'] extendsS ?T :T extends [inferF , ...inferR ]?F extends any[]? [...FlattenDepth <F ,S , [...U , 1]>, ...FlattenDepth <R ,S ,U >]: [F , ...FlattenDepth <R ,S ,U >]:T
ts
// most populartypeFlattenDepth <T extends any[],S extends number = 1,U extends any[] = []> =U ['length'] extendsS ?T :T extends [inferF , ...inferR ]?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次递归*/typeNumToArr <N extends number = 1,U extends any[] = []> =N extendsU ['length']?U :U ['length'] extends 50?U :NumToArr <N , [...U ,N ]>;typeNumMinusOne <T extends any[]> =T ['length'] extends 1? 0:T extends [inferF , ...inferRest ]?Rest ['length']: 0;typedesct <T extends any[],U extends any[] = []> =T extends [inferF , ...inferRest ]?desct <Rest , [...U ,...(F extends any[] ?F : [F ])]>:U ;typeFlattenDepth <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次递归*/typeNumToArr <N extends number = 1,U extends any[] = []> =N extendsU ['length']?U :U ['length'] extends 50?U :NumToArr <N , [...U ,N ]>;typeNumMinusOne <T extends any[]> =T ['length'] extends 1? 0:T extends [inferF , ...inferRest ]?Rest ['length']: 0;typedesct <T extends any[],U extends any[] = []> =T extends [inferF , ...inferRest ]?desct <Rest , [...U ,...(F extends any[] ?F : [F ])]>:U ;typeFlattenDepth <T extends any[],D extends number = 1,U extends any[] =T > =D extends 0?U :FlattenDepth <desct <U >,NumMinusOne <NumToArr <D , []>>>;