[英]How to read arrow function type definition in typescript
When I try to develop my app by serverless-framework当我尝试通过无服务器框架开发我的应用程序时
Now I stuck in typescript specific issues.现在我陷入了打字稿的具体问题。
my question is const hello: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (event) =>
.我的问题是
const hello: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (event) =>
。
so hello
function is defined.所以定义了
hello
函数。 but what is ValidatedEventAPIGatewayProxyEvent
?但什么是
ValidatedEventAPIGatewayProxyEvent
?
it defined return type?它定义了返回类型? or input value type ?
或输入值类型?
How can I read them correctly?我怎样才能正确阅读它们?
I searched about them but I couldn't find ts document about it.我搜索了它们,但找不到关于它的 ts 文档。 if someone has opinion of materials will you please let me know.
如果有人对材料有意见,请告诉我。 Thanks
谢谢
handler.ts
import type { ValidatedEventAPIGatewayProxyEvent } from '@libs/api-gateway';
import { formatJSONResponse } from '@libs/api-gateway';
import { middyfy } from '@libs/lambda';
import schema from './schema';
const hello: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (event) => {
return formatJSONResponse({
message: `Hello ${event.body.name}, welcome to the exciting Serverless world!`,
event,
});
};
export const main = middyfy(hello);
schema.ts
export default {
type: "object",
properties: {
name: { type: 'string' }
},
required: ['name']
} as const;
api-gateway.ts
import type { APIGatewayProxyEvent, APIGatewayProxyResult, Handler } from "aws-lambda"
import type { FromSchema } from "json-schema-to-ts";
type ValidatedAPIGatewayProxyEvent<S> = Omit<APIGatewayProxyEvent, 'body'> & { body: FromSchema<S> }
export type ValidatedEventAPIGatewayProxyEvent<S> = Handler<ValidatedAPIGatewayProxyEvent<S>, APIGatewayProxyResult>
export const formatJSONResponse = (response: Record<string, unknown>) => {
return {
statusCode: 200,
body: JSON.stringify(response)
}
}
If you write const test:number = 1
typescript assumes that test is of type number.如果你写
const test:number = 1
typescript 假设 test 是 number 类型。 In your case it defines the type of the arrow function not return type.在您的情况下,它定义了箭头函数的类型而不是返回类型。
Like this example: const test:(e:unknown) => number = (e:unknown) => 1
像这个例子:
const test:(e:unknown) => number = (e:unknown) => 1
This type after the colon defines that the arrow method has one argument of type unknown and returns a number.冒号后的这种类型定义箭头方法有一个未知类型的参数并返回一个数字。
Serverless Framework provides types for all of their methods that you handler matches the definition of their type.无服务器框架为您处理的所有方法提供类型,以匹配其类型的定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.