typescript 在打字稿中,如何定义异步函数的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38744159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:15:51  来源:igfitidea点击:

In typescript, how to define type of async function

typescript

提问by Ron

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?

谁能帮我解决这个问题?

回答by Thiago Barcala

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

发现这个搜索如何为异步箭头函数声明“typedef”。

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

如果你只是将函数的返回类型声明为一个 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.

Microsoft 的 TS Linter 将推荐第二种语法。

回答by Motti

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

async关键字用于向编译器/运行时指示相关函数将在await内部使用(因此它可以放入所需的脚手架以启用它)。

This means that asynconly has meaning for the implementationof the function, not it's interface. Therefore having asyncon 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).

这意味着async它只对函数的实现有意义,而不是它的interface。因此,async拥有接口的方法没有用,您想说该函数返回某个Promise(在您的情况下Promise<string>),但您不想强制接口的实现者以某种方式(使用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.thenor perhaps even some new methodology that will come up in the future.

然后,选择实现此功能的任何人都可以选择使用async,简单的旧方法Promise.then或什至将来会出现的一些新方法。

回答by Ben Carp

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

将返回对象的类型传递给 Promise 泛型。

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

Alternatively you can declare an AsyncFunctiongeneric type.

或者,您可以声明一个AsyncFunction泛型类型。

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.

AsyncFunction 是一个类型泛型,它接收两个类型变量——输入(A)的类型和输出的类型。

回答by Jeff Gu Kang

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
  }
}