简体   繁体   English

有人可以解释一下这个 typescript 方法签名吗?

[英]Can someone explain me this typescript method signature?

I have started off with the following lambda function in typescript:我从 typescript 中的以下 lambda function 开始:

export const handler: APIGatewayProxyHandler = async (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> => {
  
}

After learning about typescript I know that the arguments in the function are given types(APIGatewayProxyEvent and Context respectively).在了解了 typescript 之后,我知道 function 中的 arguments 是给定的类型(分别为 APIGatewayProxyEvent 和 Context)。 Similarly the function's return type is defined by adding it at the end after a colon (Promise<APIGatewayProxyResult>) .类似地,函数的返回类型是通过在冒号(Promise<APIGatewayProxyResult>)后面添加来定义的。

There's still a type being used here: handler: APIGatewayProxyHandler这里仍然使用了一个类型: handler: APIGatewayProxyHandler

What does this type signify?这种类型意味着什么? What is it known as?它被称为什么? It's definitely not the return type of the method then what is it?它绝对不是方法的返回类型,那它是什么?

I have scoured through various typescript blogs and still couldn't find any information about this.我浏览了各种 typescript 博客,但仍然找不到任何相关信息。

Thanks!谢谢!

Functions can also have types, for example:函数也可以有类型,例如:

type MyFunc = (a: number) => string;

Then when you create a function, you can assign it that type:然后,当您创建 function 时,您可以为其分配该类型:

const actualFunction: MyFunc = (a: number): string {
  return 'hi';
}

Giving the constant a type enforces that your argument and return types match the signature of the type, and if you accidentally messed up the return type, having APIGatewayProxyHandler there will cause typescript to complain.给常量一个类型强制你的参数和返回类型匹配类型的签名,如果你不小心弄乱了返回类型,有APIGatewayProxyHandler将导致 typescript 抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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