middy icon indicating copy to clipboard operation
middy copied to clipboard

`Argument of type 'ScheduledHandler' is not assignable to parameter of type 'MiddyInputHandler<EventBridgeEvent<"Scheduled Event"`

Open garysassano opened this issue 1 year ago • 1 comments

Describe the bug A clear and concise description of what the bug is.

To Reproduce How to reproduce the behaviour:

index.ts

import { Logger } from "@aws-lambda-powertools/logger";
import { injectLambdaContext } from "@aws-lambda-powertools/logger/middleware";
import { ScheduledHandler } from "aws-lambda";
import middy from "@middy/core";
import httpErrorHandler from "@middy/http-error-handler";

const logger = new Logger();

const lambdaHandler: ScheduledHandler = async () => {
  const response = {
    statusCode: 200,
    body: "Hello from Lambda!",
  };

  console.log(response);
};

export const handler = middy()
  .use(injectLambdaContext(logger, { logEvent: true }))
  .use(httpErrorHandler())
  .handler(lambdaHandler);

Expected behaviour No error should be thrown, instead you get this:

Argument of type 'ScheduledHandler' is not assignable to parameter of type 'MiddyInputHandler<EventBridgeEvent<"Scheduled Event", any>, void, Context>'.
  Types of parameters 'callback' and 'opts' are incompatible.
    Type 'MiddyHandlerObject' is not assignable to type 'Callback<void>'.
      Type 'MiddyHandlerObject' provides no match for the signature '(error?: string | Error | null | undefined, result?: void | undefined): void'.

Environment (please complete the following information):

  • Node.js: 20.17.0
  • Middy: 5.4.7
  • AWS SDK: 3.644.0

Additional context

You can fix the issue by avoiding to use the .handler(), like this:

export const handler = middy(lambdaHandler)
  .use(injectLambdaContext(logger, { logEvent: true }))
  .use(httpErrorHandler())

garysassano avatar Sep 03 '24 23:09 garysassano