타입 챌린지 4471 - Zip

소파의 벨로그·2025년 4월 25일

타입챌린지

목록 보기
65/131

문제 링크

문제

이 도전에서, 당신은 T와 U가 튜플형태인 Zip<T, U> 타입을 구현해야한다.

In This Challenge, You should implement a type Zip<T, U>, T and U must be Tuple

내 풀이

type ZipImplement<T extends any[],U extends any[],Result extends any[]=[]>=
  T['length'] extends Result['length']?Result:
  U['length'] extends Result['length']?Result:
  ZipImplement<T,U,[...Result,[T[Result['length']],U[Result['length']]]]>


type Zip<T extends any[], U extends any[]> = ZipImplement<T,U>

간단한 배열 재귀형태의 문제이다.
다만, 사례에서 T와 U의 길이가 다를 경우 끊어야 한다는 제한이 있어 제한을 두었다

다른 사람의 풀이

다른 풀이

type Zip<A extends any[], B extends any[], L extends any[] = []> = L['length'] extends A['length'] | B['length']
  ? L
  : Zip<A, B, [...L, [A[L['length']], B[L['length']]]]>

간단하게 union 타입을 써서 줄인 경우도 있었다.

참고자료

https://github.com/type-challenges/type-challenges/issues/5619

0개의 댓글