gvergnaud / hotscript

A library of composable functions for the type-level! Transform your TypeScript types in any way you want using functions you already know.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposal for Zip

eloytoro opened this issue · comments

Nice, thanks for implementing this Eloy!

I was thinking about implementing a ZipWith function too that would take a callback as parameter, similar to lodash's zipWith, and implement Zip using it. Do you want to give it a shot?

commented

I'll do this one, transforming this proposal to a tail recursive optimized one.
image

type ZipImpl<arr1 extends unknown[],arr2 extends unknown[], acc extends unknown[]= []> =
  [arr1, arr2] extends [[infer item1, ...infer rest1], [infer item2, ...infer rest2]]
      ? ZipImpl<rest1, rest2,[...acc,[item1, item2]]>
      : acc

interface Zip extends Fn {
  output: this["input"] extends [infer arr1 extends unknown[], infer arr2 extends unknown[]]
    ? ZipImpl<arr1,arr2>
    : never
}
commented

Done on #26