Ron Ron - 4 months ago 14
TypeScript Question

In typescript, how to define type of async function

I tried to define a type of async function, but failed in compilation, see below:

interface SearchFn {
async (subString: string): string;
}

class A {
private Fn: SearchFn
public async do():Promise<string> {
await this.Fn("fds") // complain here: cannot invoke an expression whose type lacks a call signature
return ''
}
}


Can anyone help me work this out?

Answer

It seems to compile for me if I just drop the async keyword from the interface:

interface SearchFn {
    (subString: string): boolean;
}

If you want the code to show that it's async, then maybe do something like:

interface SearchFn {
    /* async */ (subString: string): boolean;
}