FlattenDepth
提出詳細
type MinusOneDigit = { "9": "8", "8": "7", "7": "6", "6": "5", "5": "4", "4": "3", "3": "2", "2": "1", "1": "0", "0": "9" } type Zero = "0" type IsZeros<T extends string> = T extends `${infer D}${infer Tail}` ? D extends Zero ? IsZeros<Tail> : false : true type ToNines<T extends string> = T extends `${string}${infer Tail}` ? `9${ToNines<Tail>}` : "" type MinusOneImpl<T extends string> = T extends `${infer Head extends keyof MinusOneDigit}${infer Tail}` ? IsZeros<Tail> extends true ? `${MinusOneDigit[Head]}${ToNines<Tail>}` : `${Head}${MinusOneImpl<Tail>}` : never type RemoveLeadingZeros<S extends string> = S extends `0${infer Tail}` ? Tail extends "" ? "0" : Tail : S type ParseInt<S extends string> = RemoveLeadingZeros<S> extends `${infer N extends number}` ? N : never type MinusOne<N extends number> = N extends 0 ? -1 : ParseInt<MinusOneImpl<`${N}`>> type FlattenOnce<T extends readonly unknown[], Acc extends readonly unknown[]=[]> = T extends [infer T1, ...infer T2] ? T1 extends readonly unknown[] ? FlattenOnce<T2,[...Acc,...T1]> : FlattenOnce<T2,[...Acc,T1]> : Acc type FlattenDepth<T extends readonly unknown[],Depth extends number=1> = Depth extends 0 ? T : FlattenOnce<T> extends T ? T : FlattenDepth<FlattenOnce<T>,MinusOne<Depth>>
提出日時 | 2023-09-15 14:30:28 |
---|---|
問題 | FlattenDepth |
ユーザー | sankantsu |
ステータス | Accepted |
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<FlattenDepth<[]>, []>>, Expect<Equal<FlattenDepth<[1, 2, 3, 4]>, [1, 2, 3, 4]>>, Expect<Equal<FlattenDepth<[1, [2]]>, [1, 2]>>, Expect<Equal<FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2>, [1, 2, 3, 4, [5]]>>, Expect<Equal<FlattenDepth<[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, [[5]]]>>, Expect<Equal<FlattenDepth<[1, [2, [3, [4, [5]]]]], 3>, [1, 2, 3, 4, [5]]>>, Expect<Equal<FlattenDepth<[1, [2, [3, [4, [5]]]]], 19260817>, [1, 2, 3, 4, 5]>>, ]