-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
「重学TS 2.0 」TS 练习题第三十七题 #57
Comments
type Flat<T extends any[]> = T extends [infer First, ...infer Rest]
? First extends any[]
? [...Flat<First>, ...Flat<Rest>]
: [First, ...Flat<Rest>]
: [];
type F0 = Flat<[]>; // []
type F1 = Flat<["a", "b", "c"]>; // ["a", "b", "c"]
type F2 = Flat<["a", ["b", "c"], ["d", ["e", ["f"]]]]>; // ["a", "b", "c", "d", "e", "f"] |
两层递归 |
|
type Flat<T extends any[]> = type F0 = Flat<[]> // [] |
type Flat<T extends any[]> = T extends [infer F, ...infer R]
? F extends any[]
? [...Flat<F>, ...Flat<R>]
: [F, ...Flat<R>]
: T; |
type Flat<T extends any[]> =
T extends [infer P1, ...infer P2]
? [...(P1 extends any[] ? Flat<P1> : [P1]), ...Flat<P2>]
: [] |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
实现一个
Flat
工具类型,支持把数组类型拍平(扁平化)。具体的使用示例如下所示:The text was updated successfully, but these errors were encountered: