Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript pick call signature from function interface

Tags:

typescript

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)?

like image 230
atomiks Avatar asked Aug 03 '26 16:08

atomiks


2 Answers

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>;
like image 114
Artem Bozhko Avatar answered Aug 08 '26 03:08

Artem Bozhko


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

like image 31
Kewin Dousse Avatar answered Aug 08 '26 03:08

Kewin Dousse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!