简体   繁体   中英

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?

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.

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.

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.

Simple Way.

export interface SignUpReturn {
  user_id: string
  platform: string
  name: string
  image_url: string
  email: string
}

export interface SignUpType {
  platform: string
  loginId: string
  password: string
  name: string
  email: string
}

const SignUp = async (userInfo: SignUpType) => {
  try {
    const data: SignUpReturn = await client.request(query, userInfo)
    return data
  } catch (error) {
    throw error
  }
}

export default SignUp

or

const SignUp = async (userInfo: SignUpType): Promise<SignUpReturn> => {
  try {
    const data = await client.request(query, userInfo)
    return data
  } catch (error) {
    throw error
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM