middy icon indicating copy to clipboard operation
middy copied to clipboard

TypeScript compile error in @middy/core 5.2.4

Open cm-rwakatsuki opened this issue 1 year ago • 2 comments

Describe the bug After updating @middy/core from 5.2.3 to 5.2.4, an error occurred in TypeScript compile (tsc).

To Reproduce How to reproduce the behaviour:

Sample code

  • packages/server/src/lambda/handlers/api-gateway/rest-api/router.ts
import serverlessExpress from "@codegenie/serverless-express";
import middy from "@middy/core";
import express from "express";

const app = express();

export const handler = middy(serverlessExpress({ app }));

Thrown error

tsc --noEmit

src/lambda/handlers/api-gateway/rest-api/router.ts:11:30 - error TS2345: Argument of type 'Handler<any, any> & ConfigureResult<any, any>' is not assignable to parameter of type 'PluginObject | MiddyInputHandler<any, any, Context> | undefined'.
  Type 'Handler<any, any> & ConfigureResult<any, any>' is not assignable to type 'MiddyInputHandler<any, any, Context>'.
    Types of parameters 'callback' and 'opts' are incompatible.
      Type 'MiddyHandlerObject' is not assignable to type 'Callback<any>'.
        Type 'MiddyHandlerObject' provides no match for the signature '(error?: string | Error | null | undefined, result?: any): void'.

11 export const handler = middy(serverlessExpress({ app }));

Expected behaviour tsc can run without errors.

Environment (please complete the following information):

  • Node.js: 20.9.0
  • Middy: 5.2.4
  • AWS SDK 2.1450.0

cm-rwakatsuki avatar Feb 15 '24 06:02 cm-rwakatsuki

Is this merge here the issue? https://github.com/middyjs/middy/pull/1165 The removal of the callback? Have the same issue with https://github.com/DefinitelyTyped/DefinitelyTyped/blob/7bd4a02ba62ede50a6198bd5b10712ae1a2fe6c8/types/aws-lambda/handler.d.ts#L84 the aws-lambda handler. Still has a callback but it doesn't match the new schema

lynxSven avatar Feb 19 '24 15:02 lynxSven

Thanks for reporting. Would someone like to put a PR together for this fix?

willfarrell avatar Feb 19 '24 18:02 willfarrell

Same issue is reproducible with the following basic Lambda using TypeScript 5.3.3:

import { type Handler } from "aws-lambda";
import middy from "@middy/core";

const baseHandler: Handler = async (event) => {
  console.log("Hello world");
};

export const handler = middy(baseHandler); // <== error here as described in issue thread

sekhavati avatar Feb 26 '24 09:02 sekhavati

I could confirm that my issue https://github.com/middyjs/middy/issues/1182 is related to this.

After downgrading to @middy/core 5.2.3, TypeScript stopped throwing the error.

garysassano avatar Feb 29 '24 06:02 garysassano

This is really frustrating, writing a proxy function just to massage the types between middy and aws lambda here means me updating a few hundred functions across our services.

https://github.com/middyjs/middy/pull/1165#issuecomment-1980921633

cmcnicholas avatar Mar 25 '24 11:03 cmcnicholas

Thanks for reporting. Would someone like to put a PR together for this fix?

@willfarrell, how would you like that PR to address and resolve this issue?

phmasek avatar Mar 25 '24 21:03 phmasek

I might have a possible solution in #1198. Is there any blessed soul willing to review it? :)

lmammino avatar Mar 25 '24 22:03 lmammino