Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ params field missing from Context type #66

Closed
eratio08 opened this issue May 9, 2020 · 4 comments
Closed

⚡ params field missing from Context type #66

eratio08 opened this issue May 9, 2020 · 4 comments
Labels
invalid This doesn't seem right

Comments

@eratio08
Copy link

eratio08 commented May 9, 2020

The params field seems to be missing from the Context type.
See https://doc.deno.land/https/deno.land/x/oak/mod.ts#Context

Currently working around this by:

router.get("/:name",  (
    ctx: oak.Context & {params: { name: string } }
) => {...});
@kitsonk
Copy link
Collaborator

kitsonk commented May 10, 2020

@eratio08 params exists only on the RouterContext, which is what is passed to router middleware. The type is inferred be default, so there should be no need to type the ctx. The following works fine:

router.get("/:name", (ctx) => {
  ctx.params; // no error
});

If you want to strongly type the context, there is a generic you can supply:

router.get<{ name: string }>((ctx) => {
  ctx.router.name;  // again, no error
});

If for some reason you want to be explicit about the type on a router context, use RouterContext instead of Context as it is also exported from the mod.ts.

@kitsonk kitsonk added the invalid This doesn't seem right label May 10, 2020
@kitsonk kitsonk closed this as completed May 10, 2020
@mkkhedawat
Copy link

Just curious, Why would we restrict anybody to be able to use params object in any other subsequent function outside router? @kitsonk

@kitsonk
Copy link
Collaborator

kitsonk commented May 10, 2020

😕 The params directly relate to the route that was parsed. Subsequent functions outside of the router wouldn't be for that route.

@mkkhedawat
Copy link

Pardon me, I misread the RouterContext.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants