Closed alexrosenfeld10 closed 8 months ago
does replacing with
router.all("/*", withFoo, withBar, withContent, (request: IRequestStrict & HasFoo & HasBar) => {
help?
discussion ongoing at https://discord.com/channels/832353585802903572/832354150884442122/1185270897158926396
So, I will attempt a back-port of as many of the v5 features (including the updated types) to v4.x... in the meantime, we have a full TS guide in the v5 docs (itty.dev/itty-router/typescript/), which should be much more helpful than what we had.
Regarding the case of this specific issue, this is really an issue oh how types are [not] inferred across an array boundary, when one item can mutate the next.
In the case of stacked middleware, I'd recommend an approach like the following:
// example types
type ARequest = { a: string } & IRequestStrict
type BRequest = { b: string } & IRequestStrict
// middleware 1
const withA: RequestHandler<ARequest> = (request) => {
request.a = 'foo'
}
// middleware 2
const withB = (request: BRequest) => { // alternative syntax
request.b = 'foo'
}
// later to actually use this safely, we can pass a generic through the whole chain
.get<ARequest & BRequest>('*', withUser, withPet, (request) => {
request.a // should find this
request.b // should find this
})
I'm going to add this specific use-case to the middleware section of the TS guide, but close this ticket in the meantime. If we run into serious issues with the v5 types (mostly identical), let's revisit or discuss on Discord!
Followup: added a section/example to: https://itty.dev/itty-router/typescript/request-handlers#example-multiple-middleware-handlers
@kwhitley thanks. If this is fixed in 5.x I wouldn't bother back porting, but that's just me - totally your prerogative. Just saying, I intend to do the 5.x upgrade soon so personally I don't mind if it's not back ported. Not sure how many others have this issue though.
Describe the Issue
Middleware functions are not typchecking properly. See discord thread for full discussion.
Example Router Code
Reproduction available at https://codesandbox.io/s/typescript-playground-forked-hwcnq5
Request Details
Steps to Reproduce
See link above
Expected Behavior
Types check properly
Actual Behavior
They do not 😢
Environment (please complete the following information):
See reproduction above. Copied here for convenience:
Additional Context
Add any other context about the problem here.