00459-medium-flatten

Back

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

Solution by HrOkiG2 #35250

type Flatten<T extends any[]> = T extends [infer First, ...infer Rest] ?
  First extends any[] ? [...Flatten<First>, ...Flatten<Rest>]
  : [First, ...Flatten<Rest>]
  : []

Solution by eunsukimme #35230

type Flatten<T extends any[], A extends any[] = []> = T extends [infer F, ...infer Rest]
  ? F extends any[]
    ? Flatten<[...F, ...Rest], A>
    : Flatten<Rest, [...A, F]>
  : A;

Solution by wendao-liu #35219

// 你的答案
type Flatten<T extends any[]> = T extends [infer A, ...infer Rest]
  ? A extends []
    ? A
    : A extends any[]
    ? Flatten<[...A, Rest]>
    : [A, ...Flatten<Rest>]
  : T;

Solution by shx123qwe #34967

type Flatten<T extends unknown[]> = T extends [infer First,...infer Rest] ? First extends unknown[] ? [...Flatten<First>,...Flatten<Rest>] : [First,...Flatten<Rest>]  : T

Solution by devshinthant #34592

type Flatten<T extends any[]> =
    T extends [infer A, ...infer R] ?
        [...(A extends any[] ? Flatten<A> : [A]), ...Flatten<R>] :
        [];

Solution by cipak #34590

// 你的答案


type Flatten<T extends any[],R extends any[] = []> = 
T extends [infer A,...infer B] ? 
A extends any[]? Flatten<[...A,...B],R>: Flatten<B,[...R,A]>
: R


Solution by Jayce-liang #34228

type Flatten<T extends any[], U extends any[] = []> = T extends [infer K, ...infer rest] ? (K extends any[] ? Flatten<rest, Flatten<K, U>> : Flatten<rest, [...U, K]>) : U

Solution by ouzexi #34015

// 思路:从左往右依次铺平第一个元素,每次递归数组外剩余部分[T[0], Flatten<T[Rest]>]
type Flatten<T extends any[]> = T extends [infer F, ...infer Rest]
 ? F extends any[] 
   ? [...F, ...Flatten<Rest>]
   : [F, ...Flatten<Rest>]
 : [...T]


 // Expect<Equal<Flatten<[1, 2, [3, 4], [[[5]]]]>, [1, 2, 3, 4, 5]>> 
 // 该断言不过,数组每个元素可能是嵌套数组,需要对第一个元素进行多次铺平 Flatten<[...F, ...Rest]
 type Flatten<T extends any[]> = T extends [infer F, ...infer Rest]
 ? F extends any[] 
   ? [...Flatten<[...F, ...Rest]>]
   : [F, ...Flatten<Rest>]
 : [...T]

Solution by ScriptBloom #33910

459 - Flatten

by zhouyiming (@chbro) #medium #array

Question

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]

View on GitHub: https://tsch.js.org/459

...

type Flatten<A extends readonly unknown[]> = A extends [infer L, ...infer R]
  ? L extends unknown[]
    ? [...Flatten<L>, ...Flatten<R>]
    : [L, ...Flatten<R>]
  : A

Solution by veralex #33784

type Flatten<T extends any[], A extends any[] = []> = T extends [infer V,...infer P] ? V extends any[] ? Flatten<[...V, ...P], A>:Flatten<[...P],[...A,V]>:A

Solution by rookiewxy #33734

// 你的答案
方案一:
type Flatten<T extends any[]> = T extends [infer A, ...infer R] ? A extends any[] ? [...Flatten<A>, ...Flatten<R>] : [A, ...Flatten<R>] : T

方案二:
type Flatten<T extends any[], R extends any[] = []> = T extends [infer A, ...infer L] ? A extends any[] ?  Flatten<[...A, ...L], [...R]>: Flatten<L, [...R, A]> : R

Solution by heyuelan #33663

// 你的答案
type Flatten<T extends unknown[], U extends unknown[] = []> = T extends [infer H, ...infer R] ? H extends unknown[] ? [...Flatten<H, [...U]>, ...Flatten<R>] : Flatten<R, [...U, H]> : U;

Solution by HelloGGG #33413

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

Solution by ZhipengYang0605 #32826

type Flatten<T extends any[]> = T extends [infer R, ...infer U] ? (R extends any[] ? [...Flatten<R>, ...Flatten<U>] : [R, ...Flatten<U>]) : []

Solution by Leen27 #32804

type Flatten<T extends unknown[]> = T extends [] 
  ? []
  : T extends [infer A, ...infer B] 
    ? A extends unknown[] 
      ? [...Flatten<A>, ...Flatten<B>]
      : [A, ...Flatten<B>]
    : []

Solution by dev-hobin #32403

type Flatten<T extends any[]> = T extends [infer first, ...infer list]
  ? first extends [...infer R]
    ? Flatten<[...R, ...list]>
    : [first, ...Flatten<list>]
  : T

Solution by nivenice #32394

type Flatten<A extends any[]> = A extends [infer F, ...infer R]
    ? F extends any[]
        ? [...Flatten<F>, ...Flatten<R>] : [F, ...Flatten<R>]
    : []

Solution by shuxiaoduo #32204

// your answers
type Flatten<T extends unknown[]> = T extends [] ? []:
                                      T extends [infer L, ...infer R] ?
                                        L extends unknown[] ? [...Flatten<L>,...Flatten<R>] : 
                                      [L,...Flatten<R>]
                                    : T

Solution by pea-sys #32160

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

Solution by jinyoung234 #31738

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

Solution by kai-phan #31642

type Flatten<T> = T extends [] 
? [] 
: T extends [infer First, ...infer Rest] 
  ? First extends unknown[] 
    ? [...Flatten<First>, ...Flatten<Rest>]
    : [First, ...Flatten<Rest>] 
  : T

Solution by SazonovV #31332

type Flatten<T extends any[], R extends any[] = []> = T extends [infer A, ...infer B] ? A extends Array<any> ? Flatten<[...A, ...B], R> : Flatten<B, [...R, A]> : R;

Solution by eward957 #31245

type Flatten<T extends any[]> = T extends [infer First, ...infer Rest]
  ? First extends any[]
    ? [...Flatten<First>, ...Flatten<Rest>]
    : [First, ...Flatten<Rest>]
  : [];

Solution by zyh-ultra #31142

type Flatten<T extends any[]> = T extends [infer F, ...infer Rest]
  ? F extends any[]
    ? Flatten<[...F, ...Rest]>
    : [F, ...Flatten<Rest>]
  : T

Solution by Minato1123 #31094

type Flatten<T extends unknown[]> =
  T extends [infer A, ...infer R]
    ? A extends unknown[]
      ? [...Flatten<A>, ...Flatten<R>]
      : [A, ...Flatten<R>]
    : [];

Solution by ricky-simple #31022

// 你的答案
type Flatten<T extends unknown[]> = T extends [infer L, ...infer Rest] ? (
  L extends unknown[] ? [...Flatten<L>, ...Flatten<Rest>]  : [L, ...Flatten<Rest>] 
) : T;

Solution by CDSP98 #31005

// your answers
type Flatten<T extends any[]> = T extends [infer F, ...infer R]
  ? F extends any[]
    ? [...Flatten<F>, ...Flatten<R>]
    : [F, ...Flatten<R>]
  : T

Solution by CharlieLoong #30939

// 解答をここに記入
type Flatten<T extends unknown[]> = T extends [infer F, ...infer R] ?  [ ...(F extends unknown[] ? Flatten<F> : [F]) , ...Flatten<R>] : []

Solution by Kakeru-Miyazaki #30858

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

Solution by leejaehyup #30827