Go Back

Refer: Medium - Flatten

Describe

In this challenge, you would need to write a type that takes an array and emitted the flatten array type.

For example:

code snippetCopytypescript
type flatten = Flatten<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, 5]

Test Cases

code snippetCopytypescript
import { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<Flatten<[]>, []>>, Expect<Equal<Flatten<[1, 2, 3, 4]>, [1, 2, 3, 4]>>, Expect<Equal<Flatten<[1, [2]]>, [1, 2]>>, Expect<Equal<Flatten<[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, 5]>>, Expect<Equal<Flatten<[{ foo: 'bar'; 2: 10 }, 'foobar']>, [{ foo: 'bar'; 2: 10 }, 'foobar']>>, ]

Solution

code snippetCopytypescript
type Flatten<T extends any[], C extends any[] = []> = T extends [infer P, ...infer K] ? P extends any[] ? Flatten<[...P, ...K], C> : Flatten<K, [...C, P]> : C