SmujMaiku
SmujMaiku

Reputation: 643

Type-able Functions From Factory Function

I'm trying to get a generic function from a factory function that can be typed later. However, when I'm defining the factory 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?

Upvotes: 0

Views: 174

Answers (1)

bogdanoff
bogdanoff

Reputation: 2358

As said in comments...

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

Upvotes: 1

Related Questions