There is a function called zip
with the following signature:
function zip<T, U, V>(ts: T[], us: U[], zipper: (t: T, u: U) => V): V[]
An attempt is made to assign a default value of (t, u) => [t, u]
to the zipper
argument:
function zip<T, U, V>(
ts: T[],
us: U[],
zipper: (t: T, u: U) => V = (t, u) => (<[T, U]>[t, u])
)
This results in a compile error stating that (T, U) => [T, U]
cannot be assigned to
(T, U) => V</code.</p>
<p>To resolve this issue, a set of overloads are used:</p>
<pre><code>export function zip<T, U>(ts: T[], us: U[]): [T, U][]
export function zip<T, U, V>(
ts: T[],
us: U[],
zipper: (t: T, u: U) => V
): V[]
export function zip<T, U>(
ts: T[],
us: U[],
zipper: (t: T, u: U) => [T, U] = (t, u) => [t, u]
): [T, U][] {
/* ... */
}
However, there are two main issues with this approach:
- The signature
zip(T[], U[]): [T, U][]
is repeated twice (first overload and the implementation itself). - The implementation's signature may not be the most general one, leading to potential errors in more complex cases.
Is there a better way to achieve the desired outcome? Is the error in the initial attempt potentially a compiler bug?