kwhitley / itty-router

A little router.
MIT License
1.7k stars 77 forks source link

Typescript router "Additional types" are ignored #202

Closed paulrostorp closed 3 months ago

paulrostorp commented 7 months ago

Describe the Issue

Following the typescript example for "additional types" (https://itty.dev/itty-router/typescript):

Example Router Code

type CF = [context: ExecutionContext];

const router = Router<IRequest, CF>();

router.get("*", (req, ctx) => {
  return new Response("foo");
});

In the code above, the type of ctx is any.

In router.handle, the type is propagated properly

Expected Behavior

The type of the second route handler argument should be of type ExecutionContext.

Actual Behavior

The type of the second route handler argument is any.

Environment (please complete the following information):

kwhitley commented 6 months ago

Weird... in our own example testbed, we have a situation that mirrors your own. We can see it working fine here: image

kwhitley commented 6 months ago

Oh wow... when switching the first param to use/union IRequestStrict (shown above) instead of IRequest (default, and what you used), it works... definitely a bug. Good catch!

kwhitley commented 6 months ago

So this stems from the following:

kwhitley commented 6 months ago

Solution

I've modified the type definition to explicitly check both the first and second param. If either have been modified, it uses the universal route.

This will roll out in 4.1x with several other changes - great catch @paulrostorp!

kwhitley commented 6 months ago

Found some issues, so this created a deep dive...

I've done a pretty heavy rework on the internals, which now allows a MUCH more robust usage.

Changes:

image

malobre commented 5 months ago

@kwhitley Are the changes you mentioned on the v4.x branch?

kwhitley commented 3 months ago

These are being included in the v5.x release that's being finalized this week (with any luck.... I just have to document the CORS changes and migration guide).

Once this is live, I'll backport the types to v4.x as well - but that said, I'd recommend migrating on to v5, as it's adding a lot of power while allowing your own code to shrink further :)

malobre commented 3 months ago

Nice, thanks, v5 is looking great !

kwhitley commented 3 months ago

Heads up - v5 is out, and we have a guide on the additional args here: https://itty.dev/itty-router/typescript/additional-arguments

I still have some work to do with animated GIFs and such showing the typing in effect, but it should all be working now, across all 3 routers! :)

kwhitley commented 3 months ago

Ofc certainly re-open if v5 is still having issues :)

Once this is stable, I'll start working backwards to patch up 4.x as much as possible, start adding deprecation notices to things like router.handle, etc. :)