Is it possible to obtain a reference to an operator (like ===
) in TypeScript?
The reason behind this question is the following function:
function dedup<T>(values: T[], equals: (a: T, b: T) => boolean): T[] {
return values.reduce<T[]>((prev, cur) => {
if (prev.filter((value) => equals(value, cur)).length == 0) prev.push(cur);
return prev;
}, []);
}
This function removes duplicates from an array based on the equals
function provided. However, when using ===
as the equality check, one has to create an unnecessary anonymous function:
dedup([1, 2, 3, 2], (a, b) => a === b)
It would be convenient to call it like this:
dedup([1, 2, 3, 2], ===)
Unfortunately, I'm unable to find a way to reference an operator such as ===
directly.