I am working with a type similar to this:
interface Test<R, T> {
inputType: R;
transformer: (input: R extends any ? R : never) => T;
}
// function for inferring type
function inferTest<T extends Test<any, any>>(t: T): T {
return t;
}
My goal is to utilize the type and function in the following manner:
const test = inferTest({
inputType: 0,
transformer: input => String(input)
})
It is crucial that everything is correctly typed, meaning that the type of transformer
should be inferred as (input: number) => string
.
However, I am facing an issue where the inference does not work as expected. The input
is being inferred as any
, possibly due to the lack of explicit typing. Is there a way to make this work so that transformer
is accurately inferred as (input: number) => string
rather than (input: any) => string
?
It's important to note that directly specifying
transformer: (input: number) => String(input)
is not a viable solution. This example simplifies the scenario, as determining the type of inputType
is much more complex in the actual use-case.