I'm in the process of developing a compact DSL for filter operations and crafting helper methods to support it.
Suppose I have a function
const equals = (left, right) => {}
This function needs to be typed so that the left
value is a field within an object, while the right
is a value of the same type as that object.
If I write
const equals = <T> (left: keyof T, right: T[keyof T]) => {}
, it almost works but the type of right
is limited to the types available on T
, rather than just the type of left
.
The desired functionality can be achieved with the following:
const equals = <T, F extends keyof T>(left: F, right: T[F])
However, this setup requires two generic parameters, disrupting the type inference flow for the surrounding code. Ideally, I would like to infer the type of the second parameter based on the first parameter. Is there a way to accomplish this?
Thank you