Suppose the following:
type GenericFunc<T> = () => Promise<T>
interface FuncWithModifier<T> extends GenericFunc<T> {
modifier: GenericFunc<T>
}
const wrapFunction = <T> (func: GenericFunc<T>): FuncWithModifier<T> => {
const modifier: GenericFunc<T> = async () => func()
return Object.assign(func, { modifier })
}
Now I can create functions with a modifier, like this:
const stringFunc: FuncWithModifier<string> = wrapFunction(async () => 'foo')
const str1: string = await stringFunc()
const str2: string = await stringFunc.modifier()
It also works without an explicit type annotation:
const implicitStringFunc = wrapFunction(async () => 'foo')
const str3: string = await implicitStringFunc()
const str4: string = await implicitStringFunc.modifier()
What I want now, is a generic function, e.g. something like:
// doesn't work, invalid syntax!
const genericFunc = <T = unknown> wrapFunction(async () => null as any)
… so that I can use it like this:
const unknown1: unknown = await genericFunc()
const unknown2: unknown = await genericFunc.modifier()
const num1: number = await genericFunc<number>()
const num2: number = await genericFunc.modifier<number>()
const bool1: boolean = await genericFunc<boolean>()
const bool2: boolean = await genericFunc.modifier<boolean>()
However, it seems that it is not possible to keep the type parameter from the called function (wrapFunction above) and instead apply it to the result. Is there another way that I could achieve this?
wrapFunctionargument. Not sure why do you need extra generic. However, if you need, you can try this, but it works only withany. I just not sure whether you want to use any or notunknownif no type parameter is given. But it should be allowed to override the return value by giving a type parameter. That should work the same for the wrapped function itself and its modifier function.