compose-middleware icon indicating copy to clipboard operation
compose-middleware copied to clipboard

Change Next err paramter to type any

Open twrayden opened this issue 5 years ago • 7 comments

Fixes: #24

I found that the type conflict comes from the err parameter type.

@types/express: (err?: any): void compose-middleware: (err?: Error | null): void

This PR changes the Next err parameter type to any which matches @types/express.

Reproduction: https://github.com/thomasraydeniscool/compose-middleware-repro

twrayden avatar Sep 16 '20 10:09 twrayden

I would have expected err | null to be assignable to any so this is odd. Can you add a test at all?

I'm a little hesitant to land this since it makes the library strictly worse for type safety.

blakeembrey avatar Oct 03 '20 04:10 blakeembrey

For example, for this to be typed correctly we'd need to change this to any: https://github.com/blakeembrey/compose-middleware/blob/c01e0de6657148540f341c7473562a8400c46f5a/src/index.ts#L13

blakeembrey avatar Oct 03 '20 04:10 blakeembrey

I took a look at your example and it's actually the bearer library that's incorrect here, not compose-middleware. The type signature says it may pass router to next(), but this library doesn't understand the magic strings that Express.js supports.

blakeembrey avatar Oct 03 '20 04:10 blakeembrey

It's caused by this line in Express.js:

(deferToNext: "router"): void;

This sounds like things work as expected, this library doesn't support deferToNext and therefore the types are working as expected. What's unexpected is probably that people are using the express.Handler definition without realizing this caveat. I'm not comfortable relaxing the typing here to support something that may actually break in production as a result (e.g. if someone actually calls next("router") and it doesn't work).

The library using express.Handler could be updated to define what it actually uses instead, or we can add next("router") support to this library instead.

blakeembrey avatar Oct 03 '20 04:10 blakeembrey

I have the same issue. Any plans to get this merged ?

menocomp avatar Dec 15 '20 03:12 menocomp

If you’d like to contribute a test case using the types I can look at it.

blakeembrey avatar Dec 15 '20 04:12 blakeembrey