Typescript类型体操 - Flatten
题目
中文
在这个挑战中,你需要写一个接受数组的类型,并且返回扁平化的数组类型。
例如:
type flatten = Flatten<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, 5]
English
In this challenge, you would need to write a type that takes an array and emitted the flatten array type.
For example:
type flatten = Flatten<[1, 2, [3, 4], [[[5]]]]> // [1, 2, 3, 4, 5]
答案
type Flatten<T extends any[]> = T extends [infer L, ...infer R]
? [...(L extends any[] ? Flatten<L> : [L]), ...Flatten<R>]
: [];