Experience this live showcase.
Presented with the code below:
type Transformer<T> = (t: T) => T;
const identity = <T>(a: T) => a;
interface HardInferenceFn {
<T>(value: T, transform: Transformer<T> | T): T
}
declare const hardInference: HardInferenceFn;
const myTransformedValue = hardInference('foo', identity);
Evidently, the type of myTransformedValue
should be string
, but it currently appears as an empty object.
After some experimentation, I discovered that including | T
around the transform parameter is causing confusion for tsc. If we remove it, then myTransformedValue will have the expected type.
What is causing this issue? Additionally, is there a method to assist tsc by specifying to infer solely based on the first parameter and not the second one, which seems to be the source of confusion?