首页 > 其他分享 >[Typescript] 65. Medium - Zip

[Typescript] 65. Medium - Zip

时间:2022-10-23 20:45:06浏览次数:42  
标签:... Typescript Zip _____________ 65 type infer Expect

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

type exp = Zip<[1, 2], [true, false]> // expected to be [[1, true], [2, false]]

 

/* _____________ Your Code Here _____________ */

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

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Zip<[], []>, []>>,
  Expect<Equal<Zip<[1, 2], [true, false]>, [[1, true], [2, false]]>>,
  Expect<Equal<Zip<[1, 2, 3], ['1', '2']>, [[1, '1'], [2, '2']]>>,
  Expect<Equal<Zip<[], [1, 2, 3]>, []>>,
  Expect<Equal<Zip<[[1, 2]], [3]>, [[[1, 2], 3]]>>,
]

 

标签:...,Typescript,Zip,_____________,65,type,infer,Expect
From: https://www.cnblogs.com/Answer1215/p/16819462.html

相关文章