Skip to content

Router.get method in Express is called with the wrong type: The request handler is mistaken for a child application #6304

Closed

Description

@hyfhaozero

Note: I use the typescript
When using the tokenRouter.get method in the auth/token.ts file to define a route handler, the TypeScript compiler throws a type mismatch error. The error message is as follows:

The console messages

auth/token.ts:15:30 - error TS2769: 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>>) => Response<...> | undefined' is not assignable to parameter of type 'Application<Record<string, any>>'.
Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>) => Response<...> | undefined' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.

15 tokenRouter.get('/getToken', function(req: Request, res: Response) {
~~~~~~~~

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

Found 1 error in auth/token.ts:15

Code part

import express, { Request, Response, Router } from 'express';

const tokenRouter: Router = express.Router();

tokenRouter.get('/getToken', function(req: Request, res: Response) {
    // 这里是处理逻辑
    const token = 'example_token';
    return res.json({ token });
});

Activity

  1. bjohansebas commented on Jan 30, 2025

    @bjohansebas
    Member

    Could you please provide the version of Express, TypeScript, and the version of the types you are using?

  2. converted this issue into a discussion #6305 on Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions