StudyPlayground / TypeScript-Exercise-Challenges

Typescript exercise & challenges
2 stars 0 forks source link

Type Challenges 459-Flatten #48

Open kscory opened 5 months ago

kscory commented 5 months ago

답안을 작성해주세요.

kscory commented 5 months ago
type Flatten<T extends unknown[], Result extends unknown[] = []> = T extends [infer F, ...infer S] 
    ? F extends [...infer U] 
        ? Flatten<[...Flatten<U>, ...S], Result>
        : Flatten<S, [...Result, F]>
    : Result;
kscory commented 5 months ago

아래가 더 나은 정답 같네요

type Flatten<T extends unknown[]> = T extends [infer F, ...infer S]
    ? F extends unknown[] 
        ? [...Flatten<[...F, ...S]>]
        : [F, ...Flatten<S>]
    : [];
bananana0118 commented 5 months ago
type Flatten<T extends any[],U extends any[] =[]> =  T extends [infer X , ...infer Y] ?
  X extends any[]?
  Flatten<[...X,...Y],U> :Flatten<[...Y], [...U, X]>
  : U
bananana0118 commented 5 months ago

아래가 더 나은 정답 같네요

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

와 이렇게 보니까 간단하네 ..

wogha95 commented 4 months ago
type Flatten<T extends any[]> = T extends [infer A, ...infer B]
  ? A extends any[]
    ? [...Flatten<[...A, ...B]>]
    : [A, ...Flatten<B>]
  : [];