04471-medium-zip

Back

type Zip<T extends any[], U extends any[]> = T extends [infer L1, ...infer Rest1]
  ? U extends [infer L2, ...infer Rest2]
    ? [[L1, L2], ...Zip<Rest1, Rest2>]
    : []
  : [];

Solution by wendao-liu #35070

type Zip<T extends unknown[], U extends unknown[]> = RemoveLastNever<{
	[K in keyof T]: K extends keyof U ? [T[K], U[K]] : never
}>

type RemoveLastNever<T extends unknown[]> = T extends [...infer R, never]
	? RemoveLastNever<R>
	: T

Solution by WAGFS #34096

type Zip<T extends any[], U extends any[], L extends any[] = []> = L['length'] extends (T['length'] | U['length'])  ? L : Zip<T, U, [...L, [T[L['length']], U[L['length']]]]>

Solution by ouzexi #34071

type Zip<T extends any[], U extends any[], A extends any[] = []> = T extends [infer S, ...infer E]? U extends [infer S2, ...infer E2] ? [[S,S2], ...Zip<E, E2>] : [] : A

Solution by rookiewxy #34059

// your answers
方案一:
type Zip<T extends any[], U extends any[], Res extends any[] = []> = Res['length'] extends T['length'] | U['length'] ? Res : 
  Zip<T, U, [...Res, [T[Res['length']], U[Res['length']]]]>

方案二:
type Zip<T extends any[], U extends any[], Res extends any[] = []> = T extends [infer A, ...infer R] ? 
  U extends [infer X, ...infer Y] ? Zip<R, Y, [...Res, [A, X]]> : Res
  : Res

Solution by heyuelan #33851

// your answers
type Zip<T extends any[], U extends any[]> =  [T, U] extends [[infer F1, ...infer R1], [infer F2, ...infer R2]]
  ? [[F1, F2], ...Zip<R1, R2>]
  : [];

Solution by pea-sys #33027

type Zip<T extends unknown[], U extends unknown[]> = [T, U] extends [[infer F1, ...infer R1], [infer F2, ...infer R2]]
  ? [[F1, F2], ...Zip<R1, R2>]
  : [];

Solution by keyurparalkar #32513

type Zip<T, U, R extends any[] = []> = T extends [infer R1, ...infer R2] ? U extends [infer R3, ...infer R4] ? Zip<R2,R4,[...R,[R1, R3]]> : R : R

Solution by Zhen-code #32008

type Zip<T extends any[], U> = T extends []
  ? []
  : U extends any[]
    ? { [P in keyof U]: P extends keyof T ? [T[P], U[P]] : [] }
    : [];

Solution by rarysson #30833

type Zip<T extends readonly any[], U extends readonly any[]> =
  [T, U] extends [[infer TF, ...infer TR], [infer UF, ...infer UR]]
  ? [[TF, UF], ...Zip<TR, UR>]
  : []

Solution by matallui #30808

Solution:

type Zip<T, U> = T extends [] 
? [] 
: U extends [] 
? [] 
: T extends [infer FirstT, ...infer RestT] 
? U extends [infer FirstU, ...infer RestU] 
? [[FirstT, FirstU], ...Zip<RestT, RestU>] : [] : [];

Solution by DmitriiBr #30285

type Zip<T extends unknown[], U extends unknown[], R extends unknown[] = []> = 
  T extends [infer A, ...infer AR]
    ? 
      U extends [infer B, ...infer BR]
        ?
          Zip<AR, BR, [...R, [A, B]]>
        : R
    : R

Solution by zhangqiangzgz #30194

type Zip<T extends unknown[], U extends unknown[]> =
  T extends [infer A, ...infer B] ?
  U extends [infer C, ...infer D] ?
  [[A, C], ...Zip<B, D>]
  : []
  : []

Solution by tclxshunquan-wang #29819

type Zip<T, U> = T extends [infer F1, ...infer R1]
  ? U extends [infer F2, ...infer R2]
    ? [[F1, F2], ...Zip<R1, R2>]
    : []
  : [];

Solution by DoubleWoodLin #28740

type Zip<
  T extends unknown[],
  U extends unknown[],
  A extends unknown[] = []
> = T extends [infer TF, ...infer TR]
  ? U extends [infer UF, ...infer UR]
    ? Zip<TR, UR, [...A, [TF, UF]]>
    : A
  : A;

Solution by DoGukKim #28475

// 你的答案

/**
 *  over array index value is undefined.
 *  //type case1 = undefined
      type case1 = [1, 2][3]
 */
type Zip<
  T extends any[],
  U extends any[],
  K extends any[] = [],
  S extends any[] = [],
> = T extends [infer A, ...infer B]
  ? U[K['length']] extends undefined
    ? S
    : Zip<B, U, [...K, 1], [...S, [A, U[K['length']]]]>
  : S

Solution by xpbsm #28466

type Zip<T extends any[], U extends any[], P extends any[] = []> = T extends [infer TF, ...infer TR] ? U extends [infer UF, ...infer UR] ? Zip<TR, UR, [...P, [TF, UF]]> : P : P

Solution by Xjc666666 #28453

type Zip<T extends unknown[], U extends unknown[]> = T extends [
  infer L,
  ...infer RestT
]
  ? U extends [infer R, ...infer RestU]
    ? [[L, R], ...Zip<RestT, RestU>]
    : []
  : [];

Solution by jiaowoxiaobala #27964

// your answers
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']]]]>

Solution by GreattitJY #27697

type Zip<T, U, Acc extends unknown[] = []> = T extends [
  infer TFirst,
  ...infer TRest
]
  ? U extends [infer UFirst, ...infer URest]
    ? Zip<TRest, URest, [...Acc, [TFirst, UFirst]]>
    : Acc
  : Acc;

Solution by alythobani #27589

type GetFirstEl<T extends any[]> = T extends [infer First, ...infer _] ? First : []

type DropFirst<T extends any[]> = T extends [infer _, ...infer Rest] ? Rest : []

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

type Zip<T extends any[], U extends any[], R extends any[] = []> = T extends [infer First, ...infer Rest]
  ? HasLength<U> extends true
    ? Zip<Rest, DropFirst<U>, [...R, [First, GetFirstEl<U>]]>
    : R
  : R

Solution by jazelly #27535

// your answers

type Zip<T extends any[], U extends any[], Z extends any[] = []> = T extends [infer TH, ...infer TRest] ? U extends [infer UH, ...infer URest] ?
Zip<TRest, URest, [...Z, [TH, UH]]>
: Z : Z

Solution by hhk9292 #27439

type Zip<T extends any[], U extends any[]> = [T, U] extends [[infer A, ...infer R1], [infer B, ...infer R2]] ? [[A, B], ...Zip<R1, R2>] : []

Solution by smileboyi #27052

type Zip<
  T extends unknown[],
  U extends unknown[],
  Result extends unknown[] = []
> = T extends [infer R, ...infer V]
  ? U extends [infer R2, ...infer V2]
    ? Zip<V, V2, [...Result, [R, R2]]>
    : Result
  : Result;

Solution by ryuji-1to #26993

type Zip <A1 extends any[], A2 extends any[]> = [[A1[0], A2[0]], [A1[1], A2[1]]]

Solution by 20yuteo #26969

type Zip<T extends any[], U extends any[], Res extends any[]=[]> =
    T extends [infer FT, ...infer LT]
      ? U extends [infer FU, ...infer LU]
        ? Zip<LT, LU, [...Res, [FT, FU]]>
        : Res
      : Res

Solution by HaoxueAllen #26631

type Zip<T extends any[], U extends any[]> = T extends [
  infer FirstT,
  ...infer RestT
]
  ? U extends [infer FirstU, ...infer RestU]
    ? [[FirstT, FirstU], ...Zip<RestT, RestU>]
    : []
  : [];

Solution by seeyoujeong #26510

type Zip<T extends unknown[], U extends unknown[]> = T extends [infer Head, ...infer Tail] ? U extends [infer HeadU, ...infer TailU] ? [[Head, HeadU], ...Zip<Tail, TailU>] : [] : []

Solution by ScarboroughCoral #26474

type Zip<T extends any[], U extends any[]> = [T, U] extends [
  [infer THead, ...infer TRest],
  [infer UHead, ...infer URest],
]
  ? [[THead, UHead], ...Zip<TRest, URest>]
  : []

Solution by valentynpodkradylin #26233


/*

使用 infer 推断类型,边界情况(为空)返回空数组,否则递归求解

*/

type Zip<T extends any[], U extends any[]> = T extends [infer A, ...infer B]
? U extends [infer C, ...infer D]
  ? [[A, C], ...Zip<B, D>]
  : []
: []

Solution by DragonnZhang #26162