I have exported a function from some module that looks like:
export function MyFunc() {
return {
foo: (in: A) => void
}
}
There is a proposal to allow using typeof with arbitrary expressions to allow things like getting the return type of a generic functions for a specific type argument (see here and here)
A more generic workaround that works today is to use a generic class with a field that is tied to the return type of the function. We can then extract the field of the class. Since for classes we can specify generic type parameters in type expressions we can extract the generic form of the return type:
export function MyFunc() {
return {
foo: (os : A) => {}
}
}
class Helper {
Return = MyFunc()
}
type FuncReturnType = Helper['Return']
type ForBool = FuncReturnType // {foo: (os: boolean) => void;}
type ForString = FuncReturnType // {foo: (os: string) => void;}
Note If you have constraints of A you will need to duplicate those on T in Helper and FuncReturnType, that is unavoidable unfortunately.