4

When we declare the type of an arrow function with generic parameters, we can do the following:

interface ArrowFunc {
    <T>(arg: T): T;
}
type ArrowFunc2 = <T>(arg: T): T;

Now I need to use this type with the generic parameter specified, how may I achieve this? Obviously type Func = ArrowFunc<T> doesn't work, since the generic parameter belongs to the function instead of the type.

4
  • Then put it on the type type ArrowFunc2<T> = (arg: T): T; Commented Nov 6, 2022 at 11:07
  • With instantiation expressions, it is possible to use ArrowFunc and create a concrete type from it where we replace T with a type of our choice as demonstrated here: tsplay.dev/WzP9LN. Is this what you want? Commented Nov 6, 2022 at 11:10
  • @Thomas The reason why I'm asking this question is that the arrow function type is imported from libraries instead of declared by my own code. The code in the question is merely a demonstration. Commented Nov 6, 2022 at 11:13
  • @TobiasS. Thanks! This seems to be a good workaround. The declared variable may be redundant, but this do solve my problem. You may put this in an answer. Commented Nov 6, 2022 at 11:16

1 Answer 1

1

We can do this with a workaround. When we declare a variable of type ArrowFunc, we can use an instantiation expression where we replace T with a type of our choice.

interface ArrowFunc {
    <T>(arg: T): T;
}

declare const fn: ArrowFunc

type Func = typeof fn<number>
// type Func = (arg: number) => number

Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.