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

Closed
dfellis opened this issue Jun 28, 2022 · 8 comments
Closed

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

dfellis opened this issue Jun 28, 2022 · 8 comments

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: [email protected]
  • 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

@Kal-Aster
Copy link

I actually encountered this same problem.
I get it tring to use the expressjwt handler on a router.

To reproduce it's simple as:

import { Router } from "express";
import { expressjwt } from "express-jwt";

Router().use(expressjwt({}));

I get this error:

No overload matches this call.
  The last overload gave the following error.
    Argument of type '{ (req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction): Promise<...>; unless: (options: Params) => { ...; }; }' is not assignable to parameter of type 'PathParams'.ts(2769)

VScode points out:
@types\express-serve-static-core\index.d.ts(153, 5): The last overload is declared here.

@jfromaniello
Copy link
Member

hey @Kal-Aster , I think I found the issue. Would you mind to try v8.5 on npm?

@Kal-Aster
Copy link

Sure, let me know when it's published

@hongbo-miao
Copy link

hongbo-miao commented Dec 9, 2024

Hi @jfromaniello could you help publish the v8.5, I believe this will help solve #348 as well.
Currently this library only works with "express": "5.0.0-beta.3", thanks!

@jfromaniello
Copy link
Member

Sorry, published!

@hongbo-miao
Copy link

hongbo-miao commented Dec 10, 2024

I can confirm the new version works well now with express v5, thank you @jfromaniello ! 😃

Tested on

  • express: 5.0.1
  • express-jwt: 8.5.1

@jfromaniello
Copy link
Member

We have some issue with this gh repo internally. I will push the changes soon here

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

4 participants