Rudi
04/25/2022, 5:33 AMRudi
04/25/2022, 5:33 AMRudi
04/25/2022, 5:35 AMChad (cysense)
04/25/2022, 5:40 AMEventBridgeHandler
. Are you following a tutorial or something for this?Ross Coundon
04/25/2022, 7:03 AMexport type EventBridgeHandler<TDetailType extends string, TDetail, TResult> = Handler<
EventBridgeEvent<TDetailType, TDetail>,
TResult
>;
So you need to pass some type information in the definition so the event itself can be typed.
e.g. if they were all strings it’d look like
export const evBridgeHandler: EventBridgeHandler<string, string, string> = (event) => {
}
Ross Coundon
04/25/2022, 7:05 AMTDetailType
, TDetail
and TResult
which means TypeScript treats the event as
EventBridgeEvent<TDetailType, TDetail>
Which in turn is used like this:
export interface EventBridgeEvent<TDetailType extends string, TDetail> {
id: string;
version: string;
account: string;
time: string;
region: string;
resources: string[];
source: string;
'detail-type': TDetailType;
detail: TDetail;
'replay-name'?: string;
}
Brinsley
04/25/2022, 8:35 AMinterface DetailType {
detailInfo: string;
}
interface Response {
field: string;
}
const p : EventBridgeHandler<"some_detail_type", DetailType, Response> = async(evt) => {
evt["detail-type"]; // "some_detail_type";
evt.detail.detailInfo; // exists
return {
field: "some return value"
};
}
Rudi
04/25/2022, 2:29 PMRudi
04/25/2022, 2:32 PMRoss Coundon
04/25/2022, 2:36 PMBrinsley
04/25/2022, 2:51 PM/**
* Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
* @see - <https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html>
*/
export type APIGatewayProxyHandler = Handler<APIGatewayProxyEvent, APIGatewayProxyResult>;
Which is why you need to tell the EventBridgeHandler
about the generic arguments:
export type EventBridgeHandler<TDetailType extends string, TDetail, TResult> = Handler<
TL;DR - it's more code because then function signature requires more code.Rudi
04/25/2022, 2:56 PMRudi
04/25/2022, 2:56 PMBrinsley
04/25/2022, 3:12 PMHandler
that APIGatewayProxyHandler
is has the more complex signature, it's just that the higher level signature is what you end up using. While the types themselves are a Typescript thing, this wouldn't necessarily be a Typescript-specific problem. It's just down to how they're intended to be used and the people that maintain them. You could run in to the same "problem" with any other typesafe language that supports generics. More than anything else it's largely just a design choice.Brinsley
04/25/2022, 3:13 PMBrinsley
04/25/2022, 3:13 PMRudi
04/25/2022, 3:13 PMBrinsley
04/25/2022, 3:36 PM