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

Express-JWT 7.x Typescript+Express Typing issues #298

Open
dfellis opened this issue Jun 28, 2022 · 1 comment
Open

Express-JWT 7.x Typescript+Express Typing issues #298

dfellis opened this issue Jun 28, 2022 · 1 comment

Comments

@dfellis
Copy link

dfellis commented Jun 28, 2022

Description

I have followed the type overriding to integrate express-jwt and jwks-rsa but the Typescript Express instructions do not work for me.

When I use the provided Request type from express-jwt, I receive the following error from the Typescript compiler:

src/router/db.ts:66:20 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type '(req: Request, res: express.Response) => Promise<express.Response<any, Record<string, any>> | undefined>' is not assignable to parameter of type 'Application<Record<string, any>>'.
      Type '(req: Request, res: Response<any, Record<string, any>>) => Promise<Response<any, Record<string, any>> | undefined>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 61 more.

66 db.post('/export', async (req: Request, res: express.Response) => {
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  node_modules/@types/express-serve-static-core/index.d.ts:174:5
    174     (path: PathParams, subApplication: Application): T;
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The last overload is declared here.

Reproduction

I suspect it is due to the difference in how we attach express-jwt to our endpoints. As we apply it to nearly all endpoints in our application if auth is enabled by the user, we placed it in an express use function to be executed on all of our /v1/ endpoints. The complaint happens in a sub-Express object that has been converted, in this case a POST endpoint on /v1/db/export.

I expect that the req variable is still being mutated correctly in v7.x, but the type is getting erased and returned back to the standard express.Request type. The @types/express-jwt package for 6.x simply mutates the global type space for Express adding an interface also named Request that contains an optional user property of an opaque User interface type.

This may be a solution here, or it may be too heavy-handed for you, but I wanted to bring it up if you can't find a proper solution to the apparent type erasure problem.

Environment

Please provide the following:

  • Version of this library used: express-jwt@7.7.5
  • Version of the platform or framework used, if applicable: Node v16.15.0, Express v4.18.1, Typescript v4.7.3
  • Other relevant versions (language, server software, OS, browser): Reproducible across multiple OSes, but I am on Ubuntu 22.04.
  • Other modules/plugins/libraries that might be involved: I am pretty sure I covered them all above.
@jfromaniello
Copy link
Member

We didn't want to polute the Request object globally in this release as this also generates some problems.

I am not sure what's the definition of your db.post or where does this comes from. Maybe if you give me more details about this I can propose other solutions.

If it is fine for you, you can extend express Request globally in your project.

Another option is to cast the object to expressJwt.Request as in const auth = (req as expressJwt.Request).auth instead of changing the signature of the handler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants