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>]
: [];
标签:...,Typescript,extends,Flatten,体操,array,type,flatten 来源: https://www.cnblogs.com/laggage/p/16663283.html