EasyPro challengeJavaScriptTypeScript
Typed Tuples — zip & unzip
TypeScriptTypesArrays
In TypeScript, tuple types are fixed-length arrays where each index has a specific type:
type Pair<A, B> = [A, B]; // position 0 = A, position 1 = B
type Triple<A, B, C> = [A, B, C]; // position 0 = A, 1 = B, 2 = C
// Destructuring preserves types:
const [name, age]: [string, number] = ['Alice', 30];
// ^ string ^ numberThe difference from a plain array:
Array<string | number>— every element isstring | number[string, number]— position 0 isstring, position 1 isnumber
Implement
function zip<A, B>(as: readonly A[], bs: readonly B[]): Array<[A, B]>
function unzip<A, B>(pairs: ReadonlyArray<readonly [A, B]>): [A[], B[]]- zip: pairs each element at the same index, stops at the shorter array
- unzip: inverse of zip — splits pairs back into two separate arrays
zip([1, 2, 3], ['a', 'b', 'c'])
// → [[1,'a'], [2,'b'], [3,'c']]
// TypeScript knows each element is [number, string] ✅
unzip([[1,'a'], [2,'b']])
// → [[1, 2], ['a', 'b']]
// TypeScript knows result is [number[], string[]] ✅Sample tests
Test #1zip of two empty arrays
Input: ["zip",[],[]]
Output: []
Test #2zip stops at shorter array
Input: ["zip",[1,2],["a","b","c"]]
Output: [[1,"a"],[2,"b"]]
Test #3unzip reconstructs both arrays
Input: ["unzip",[[1,"a"],[2,"b"],[3,"c"]]]
Output: [[1,2,3],["a","b","c"]]
Test #4unzip of empty array
Input: ["unzip",[]]
Output: [[],[]]
Test #5zip pairs elements
Input: ["zip",[1,2,3],["a","b","c"]]
Output: [[1,"a"],[2,"b"],[3,"c"]]