At the moment, TypeScript does not fully support higher kinded types in the type system, which are necessary for representing an operation on a generic function type. In short, you won't be able to achieve this currently.
To work around this limitation, one approach is to manually specify concrete types that you hope would be generic, or trick the compiler into going through the required analysis by simulating some runtime effects. It's difficult to determine your exact use case from the example code provided, so the following workarounds may seem unconventional, but it could potentially work for your situation.
In the case of manually specifying types, your example code might end up looking something like this:
type T1 = { a: number };
Alternatively, if you want to pretend to have runtime code, you could try something along these lines:
const t1 = true as false || f1(null! as number);
type T1 = typeof t1; // {a: number}
In this scenario, I've misled the compiler by assigning true
the type false
, causing the expression t1
to simply short-circuit to true
at runtime and avoid invoking f1()
. However, the compiler sees it as false || f1(someNumber)
during compilation, hence expecting t1
to have the type {a: number}
, as intended.
There was a proposal to allow the type query operator typeof
to work on any expression, eliminating the need for runtime code altogether. Unfortunately, this proposal was rejected, so the workaround mentioned earlier remains valid.
I apologize for not having a more straightforward solution for you. I hope this information proves helpful to you. Best of luck!