middy icon indicating copy to clipboard operation
middy copied to clipboard

`.use()` does not intersect Typescript types appropriately for an array of middleware

Open parker-norwood opened this issue 1 year ago • 4 comments

Describe the bug The types of middlewareObj<Event> generics do not correctly intersect when attaching an array of middlewares via the .use([middleware1(), middleware2()]) function.

To Reproduce How to reproduce the behaviour:

import middy from "@middy/core";
import httpEventNormalizer from "@middy/http-event-normalizer";
import httpJsonBodyParser from "@middy/http-json-body-parser";

middy()
  .use(httpJsonBodyParser())
  .use(httpEventNormalizer())
  .handler((event, context) => {
    /* 
      typescript hint for event (intersection of all middlewareObj<Event> generics):

      (parameter) event: APIGatewayProxyEvent & {
          multiValueQueryStringParameters: APIGatewayProxyEventMultiValueQueryStringParameters;
          pathParameters: APIGatewayProxyEventPathParameters;
          queryStringParameters: APIGatewayProxyEventQueryStringParameters;
      } & RequestEvent
    */

    return {};
  });

middy()
  .use([httpJsonBodyParser(), httpEventNormalizer()])
  .handler((event, context) => {
    /*
      typescript hint for event (*only* the first middlewareObj<Event>'s generic type):

      (parameter) event: APIGatewayProxyEvent & {
          multiValueQueryStringParameters: APIGatewayProxyEventMultiValueQueryStringParameters;
          pathParameters: APIGatewayProxyEventPathParameters;
          queryStringParameters: APIGatewayProxyEventQueryStringParameters;
      }
    */

    return {};
  });

Expected behaviour Regardless of using middy().use(middleware1()).use(middleware2()) or middy().use([middleware1(), middleware2()]) types should intersect correctly.

Environment (please complete the following information):

  • Node.js: v22.14.0
  • Middy: 6.1.6
  • AWS SDK: N/A

Additional context

This also seems to happen in Middy 5.X.X

parker-norwood avatar Mar 28 '25 03:03 parker-norwood

A PR to cover this use case is welcome.

willfarrell avatar Mar 28 '25 14:03 willfarrell

I can take it 🙏

Will take a look ASAP and keep you posted

naorpeled avatar Apr 01 '25 23:04 naorpeled

Started working on this, still need to figure something out with param propagation when chaining several .use calls.

Once I find a solution for that we'll be good to go.

naorpeled avatar Apr 02 '25 21:04 naorpeled

Hey, updating that I found some issues with my implementation, so I'll need to re-think the solution.

I'm not sure if I'll be able to get back to this before the next weekend but will keep you posted.

If anyone else wants to take this feel free 🙏

naorpeled avatar Apr 05 '25 20:04 naorpeled

Closing, inactivity

willfarrell avatar Aug 24 '25 19:08 willfarrell

Why is this being closed? This is a critical bug in the typing system for the core functionality in this library.

parker-norwood avatar Aug 24 '25 19:08 parker-norwood

PRs to improve types are always welcome. We've started work on v7, so if a breaking change is needed to address this. Now is the time.

willfarrell avatar Aug 24 '25 19:08 willfarrell