Hi, I'm struggling to make typescript happy for a ...
# help
r
Hi, I'm struggling to make typescript happy for a simple EventBridgeHandler function .. I hope someone can point out where I'm going wrong:
These are the errors:
I'm no typescript guru, but it looks correct, I can't see where I'm going wrong. Hoping someone can show me how they code up their typescript EventBridge handler lambdas. Many thanks.
c
Hmmmm, I can tell you how to fix the type errors, but I don't think you should be using
EventBridgeHandler
. Are you following a tutorial or something for this?
r
I’m not that familiar with EventBridge but looking at it the type def looks like this:
Copy code
export 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
Copy code
export const evBridgeHandler: EventBridgeHandler<string, string, string> = (event) => {

}
So it expects the type for
TDetailType
,
TDetail
and
TResult
which means TypeScript treats the event as
EventBridgeEvent<TDetailType, TDetail>
Which in turn is used like this:
Copy code
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;
}
b
Super quick code overview of how this looks:
Copy code
interface 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"
    };
}
r
@Chad (cysense) Yes, but it doesn't mention EventBridge only APIGateway and SNS. It look really straightforward https://serverlessfirst.com/aws-lambda-type-definitions/ I want to type a Lambda EB event handler
@Ross Coundon @Brinsley Thanks a lot for the code samples, I get it but it's way more code than the examples in the blog post I just mentioned previous to this msg.
r
It shouldn’t be a lot more You just need to pass the appropriate types when defining your handler so TypeScript knows what to do with them. Without them, TS wouldn’t have any way to provide you any type checking since an eventbridge event could be anything
b
Looking at the docs for the API gateway handler, it doesn't have the same sort of generic signature:
Copy code
/**
 * 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:
Copy code
export type EventBridgeHandler<TDetailType extends string, TDetail, TResult> = Handler<
TL;DR - it's more code because then function signature requires more code.
r
Hmm .. Odd how the func signature is "more complex" than APIGateway. Typescript forces you do dig deeper doesn't it.
I assumed EB would be more or less the same at APIG
b
Well, it's just the types. If you look at the
Handler
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.
If it really grinds your gears you could write you own type def. that extends the `EventBridgeHandler`and populates those generics on the subclass in whatever uniform way you intend to use it.
"six of one" and all that.
r
Interesting, thx for taking the time to explain. I'm TS noob learning ..
b
No worries, we were all there at some point 🙂