Skip to content
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

Open
semlinker opened this issue Sep 25, 2021 · 7 comments
Open

「重学TS 2.0 」TS 练习题第三十七题 #57

semlinker opened this issue Sep 25, 2021 · 7 comments

Comments

@semlinker
Copy link
Owner

实现一个 Flat 工具类型,支持把数组类型拍平(扁平化)。具体的使用示例如下所示:

type Flat<T extends any[]> = // 你的实现代码

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"]

请在下面评论你的答案

@zhaoxiongfei
Copy link

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"]

@mingzhans
Copy link

type Flat<T extends any[], S extends any[] = []> = T extends [infer R, ...infer Rest]?
  R extends any[] ?
  Flat<Rest, Flat<R, S>> : 
  Flat<Rest, [...S, R]> : 
  S// 你的实现代码
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"]

两层递归

@Cxdhz
Copy link

Cxdhz commented Nov 23, 2021

type Flat<T extends any[]> = 
    T extends [infer A, ...infer B] 
    ? [...(A extends any[] ? Flat<A> : [A]), ...Flat<B>] 
    : []

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"]

@jackwangwj
Copy link

type Flat<T extends any[]> =
T extends [infer A, ...infer Rest]
? A extends [...infer Rest2]
? [...Flat, ...Flat]
: [A, ...Flat]
: T
// 你的实现代码

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"]

@zjxxxxxxxxx
Copy link

type Flat<T extends any[]> = T extends [infer F, ...infer R]
  ? F extends any[]
    ? [...Flat<F>, ...Flat<R>]
    : [F, ...Flat<R>]
  : T;

@bill-lai
Copy link

type Flat<T extends any[]> = 
  T extends [infer P1, ...infer P2]
    ? [...(P1 extends any[] ? Flat<P1> : [P1]), ...Flat<P2>]
    : []

@dentm
Copy link

dentm commented Jun 24, 2022

type Flat<T extends any[], U extends any[] = []> =
    T['length'] extends 0 
    ? U 
    : T extends [infer X, ...infer B] 
    ? (X extends [infer D, ...infer E] ? Flat<[...E, ...B], [...U, D]>  : Flat<B, [...U, X]> ) 
    : U

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"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants