My goal is to create a generic function from a factory function that can be typed later. However, I am encountering an issue where I am required to define the return type of the factory function at the time of its definition:
export type TypeFunction<T> = (value: T) => T;
export type GeneratorFunction = {
typeFunction: TypeFunction,
// Generic type 'TypeFunction' requires 1 type argument(s).ts(2314)
}
export function generatorFunction(): GeneratorFunction {
// ...
return { typeFunction };
}
Ideally, I would like to be able to call the returned typeFunction
with different types such as string or other custom types in the following manner:
const { typeFunction } = generatorFunction();
const s = typeFunction<string>('string');
const o = typeFunction<OtherType>(other);
I am trying to figure out how to pass along this flexibility with typing downstream. Can anyone help me with this?