`.use()` does not intersect Typescript types appropriately for an array of middleware
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
A PR to cover this use case is welcome.
I can take it 🙏
Will take a look ASAP and keep you posted
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.
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 🙏
Closing, inactivity
Why is this being closed? This is a critical bug in the typing system for the core functionality in this library.
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.