7

There is a function interface with statics:

interface MyFunction {
  (value: string): string;
  a: string;
  b: string;
}

How do I Pick the call signature only (ignore a and b)?

2 Answers 2

6

You cannot Pick a call signature as it is not a property of your interface.
You can do next:

interface MyFunction {
    (value: string): string;
    a: string;
    b: string;
}

type Callable<T> = T extends (...args: any[]) => any ? (...args: Parameters<T>) => ReturnType<T> : never;

type MyFunctionCallSignature = Callable<MyFunction>;
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't know the type Parameters existed. When did it get introduced ?
Parameters was released in 3.1 according to this typescriptlang.org/docs/handbook/…
1

You can't Pick it because you can't pass a string key that will select it, however here are constructed types that will infer the correct type of your Function :

interface MyFunction {
  (value: string): string;
  a: string;
  b: string;
}

type SignatureType<T> = T extends (...args: infer R) => any ? R : never;
type CallableType<T extends (...args: any[]) => any> = (...args: SignatureType<T>) => ReturnType<T>;

type CallableOfMyFunction = CallableType<MyFunction>; // Type (value: string) => string

Playground link

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.