In typescript, how to define type of async function

Typescript

Typescript Problem Overview


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?

Typescript Solutions


Solution 1 - Typescript

Found this searching how to declare a "typedef" for an async arrow function.

It works if you just declare the return type of the function to be a Promise:

interface SearchFn {
    (subString: string): Promise<boolean>;
}

or as a type declaration:

type SearchFn = (subString: string) => Promise<boolean>;

Microsoft's TS Linter will recommend this second syntax.

Solution 2 - Typescript

The async keyword is used to indicate to the compiler/runtime that the function in question will use await internally (so it can put in the required scaffolding to enable it).

This means that async only has meaning for the implementation of the function, not it's interface. Therefore having async on an interface's method isn't useful, you want to say that the function returns a certain Promise (in your case Promise<string>) but you don't want to enforce that the interface's implementer implements this in a certain way (using await).

So as other said before me:

interface SearchFn {
    (subString: string): Promise<string>;
}

Then, whoever chooses to implement this function can choose to use async, plain old Promise.then or perhaps even some new methodology that will come up in the future.

Solution 3 - Typescript

Pass the type of the returned object to the Promise generic.

type SearchFn = (subString: string): Promise<string>;

Alternatively you can declare an AsyncFunction generic type.

type AsyncFunction <A,O> = (...args:A) => Promise<O> 
type SearchFn = AsyncFunction<[string], string>

AsyncFunction is a type generic that receives two type variables - the type of the input(A), and the type of the output.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRonView Question on Stackoverflow
Solution 1 - TypescriptThiago BarcalaView Answer on Stackoverflow
Solution 2 - TypescriptMottiView Answer on Stackoverflow
Solution 3 - TypescriptBen CarpView Answer on Stackoverflow