简体   繁体   中英

Can someone explain me this typescript method signature?

I have started off with the following lambda function in typescript:

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). Similarly the function's return type is defined by adding it at the end after a colon (Promise<APIGatewayProxyResult>) .

There's still a type being used here: 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.

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:

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.

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