Let's say we have a generic interface
interface CompareOp<T>{
a: T;
b: T;
cmp: (v1: T, v2: T) => number;
}
I'm looking for a way to create a type for an array of CompareOp of any type. For example:
// valid, each compare function receives the type of its fields
[
{a: 1, b:1, cmp: (v1: number, v2: number) => v1 - v2},
{a: 'a', b: 'b', cmp: (v1: string, v2: string) => v1.localCompare(v2)}
]
// invalid, compare function does not match fields
[
{a: 1, b:1, cmp: (v1: string, v2: string) => v1.localCompare(v2),
{a: 'a', b: 'b', cmp: (v1: number, v2: number) => v1 - v2}
]
Is there a way to express the type of this array in TypeScript?