Mateja Petrovic
Mateja Petrovic

Reputation: 4357

Typescript nested type variables

Have this:

type Format<A, R> = (arg: A) => R;

type FormatString<R> = (str: string) => R;

type FormatNumber<R> = (num: number) => R;

Would like to have this:

type Format<A<R>> = (arg: A) => R;

type FormatString = Format<string>;

type FormatNumber = Format<number>;

Or this:

type Format<A, R> = (arg: A) => R;

type FormatString = Format<string>;

type FormatNumber = Format<number>;

Currying or partial application for types is what I'm looking for. Or maybe some other workaround which keeps the code dry.

Upvotes: 0

Views: 153

Answers (1)

Joseph Sikorski
Joseph Sikorski

Reputation: 715

Just add R as another type parameter on the partially specialized types.

type Format<A, R> = (arg: A) => R;

type FormatString<R> = Format<string, R>;

type FormatNumber<R> = Format<number, R>;

Upvotes: 3

Related Questions