Description
TypeScript Version: 3.2.4
Search Terms: generics, strict equality, overlap
Code
Here is a function I wrote, to serve as a repro case.
export function arraysEqual<A, B>(a: A[], b: B[]): boolean {
if(a.length !== b.length) return false;
for(let i = 0; i < a.length; i++) {
if(a[i] !== b[i]) return false;
}
return true;
}
Expected behavior:
The function compiles. It accepts two arrays of any two types and performs a strict equality check on their elements, whether A and B are the same type or not.
Actual behavior:
Compilation error on the line if(a[i] !== b[i]) return false;
-
This condition will always return 'true' since the types 'A' and 'B' have no overlap.
A and B could very well be the same type. The error isn't logical.
I will probably just rewrite it to take a single generic parameter T and both arrays will be of type T[], since the function should work just as well for me in this case. But I still believe that this behavior qualifies as a bug.