FlattenDepth
提出詳細
type LUT = [9, 0, 1, 2, 3, 4, 5, 6, 7, 8] type Reverse<T extends string, O extends string = ""> = T extends `${infer L}${infer R}` ? Reverse<R, `${L}${O}`> : T extends string ? `${T}${O}` : O type MinusOne_<T extends string> = Reverse<T> extends `${infer Digit extends number}${infer Rest}` ? Digit extends 0 ? `${MinusOne_<Reverse<Rest>>}9` : `${Reverse<Rest>}${LUT[Digit]}` : never type TrimZero<T extends string> = T extends `${infer H extends number}${infer R}` ? R extends "" ? `${H}` : H extends 0 ? TrimZero<R> : T : T type ParseInt<T extends string> = T extends `${infer N extends number}` ? N : never type MinusOne<T extends number> = ParseInt<TrimZero<MinusOne_<`${T}`>>> type Flat<T extends unknown[], _Acc extends unknown[] = []> = T extends [infer L, ...infer R] ? L extends unknown[] ? Flat<R, [..._Acc, ...L]> : Flat<R, [..._Acc, L]> : _Acc type FlattenDepth<T extends unknown[], D extends number = 1> = D extends 0 ? T : T extends Flat<T> ? Flat<T> : FlattenDepth<Flat<T>, MinusOne<D>>
提出日時 | 2024-09-11 11:45:07 |
---|---|
問題 | FlattenDepth |
ユーザー | ookkoouu |
ステータス | 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]>>, ]