Home > Net >  Type-able Functions From Generator Function
Type-able Functions From Generator Function

Time:10-05

I'm trying to get a generic function from a generator function that can be typed later. However, when I'm defining the generator function return I'm being forced to type it then:

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'd like to be able to call that returned typeFunction with the appropriate type be it string or otherwise like so:

const { typeFunction } = generatorFunction();
const s = typeFunction<string>('string');
const o = typeFunction<OtherType>(other);

How do I pass the ability to set this typing downstream?

CodePudding user response:

As said in comments...

type TypeFunction = <T>(value: T) => T
  • Related