This is a simple attempt to use an overloaded function as an equality constraint for a generic higher order function:
function eq(x: boolean, y: boolean): boolean;
function eq(x: number, y: number): boolean;
function eq(x: string, y: string): boolean;
function eq(x: any, y: any) { return x === y };
const arrEq = <A>(eq: (x: A, y: A) => boolean) => (xs: A[]) => (ys: A[]) => {
const go = (b: boolean, i: number): boolean =>
b === false ? false
: i === xs.length ? true
: go(eq(xs[i], ys[i]), i + 1);
return go(true, 0)
};
arrEq(eq) ([1, 2, 3]) ([1, 2, 3]); // A
In line A
the type variable A
is inferred as string
, which seems quite arbitrarily. How can I fix this?