Created
March 24, 2023 09:11
-
-
Save T1T4N/99ba85d12ddfc6c43e03eddcf6282f96 to your computer and use it in GitHub Desktop.
Typescript implementation of a zip function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://stackoverflow.com/a/70192772/6053417 | |
export function zip<T extends unknown[][]>( | |
...args: T | |
): { [K in keyof T]: T[K] extends (infer V)[] ? V : never }[] { | |
const minLength = Math.min(...args.map((arr) => arr.length)); | |
// @ts-expect-error This is too much for ts | |
return [...Array(minLength).keys()].map((i) => args.map((arr) => arr[i])); | |
} | |
export function zip2<T extends unknown[][]>( | |
...args: T | |
): { [K in keyof T]: T[K] extends (infer V)[] ? V | undefined : never }[] { | |
const maxLength = Math.max(...args.map((arr) => arr.length)); | |
// @ts-expect-error This is too much for ts | |
return [...Array(maxLength).keys()].map((i) => args.map((arr) => arr[i])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment