Definitelytyped: @types/express, @types/express-serve-static-core -- Basic server fails. 'Argument of type '"/api"' is not assignable to parameter of type 'RequestHandlerParams'.

Created on 4 Aug 2017  ·  3Comments  ·  Source: DefinitelyTyped/DefinitelyTyped

  • [x ] I tried using the @types/express and @types/express-serve-static-core package and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [ ] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @borisyankov

Code:

import * as express from "express";
import { Request, Response } from "express";
import * as bodyParser from "body-parser";
import * as api from "./routes/api";

namespace my_api  {
    let app = express();
    let port: number = process.env.port || 3000;
    app.use(bodyParser.json());
    app.use('/api', api);
    app.get('/', (req: Request, res: Response) => {
           res.send("Hello world");
    });

    app.listen(port, () => console.log(`Express app listening on port ${port}`);
}

When building:

ERROR in /path/to/server.ts (25,11): Argument of type '"/api"' is not assignable to parameter of type 'RequestHandlerParams'.

Related:
expressjs/express#3263

Most helpful comment

hi @natejgardner
I encountered this error too and spent way too much time figuring this out. I tried fixing types definition by writing own d.ts. files but I failed. Then I came across this expressjs: typescript: Argument of type 'typeof ' is not assignable to parameter of type 'RequestHandlerParams'
I see a lot of problems with TypeScript types and Expressjs and maybe this issue is serious but importing without using * seems to solve problem at least with calling overloaded use function.

All 3 comments

hi @natejgardner
I encountered this error too and spent way too much time figuring this out. I tried fixing types definition by writing own d.ts. files but I failed. Then I came across this expressjs: typescript: Argument of type 'typeof ' is not assignable to parameter of type 'RequestHandlerParams'
I see a lot of problems with TypeScript types and Expressjs and maybe this issue is serious but importing without using * seems to solve problem at least with calling overloaded use function.

@natejgardner your problem is into the Api Router:

you can try a solution like this (into "./routes/api" file):

import { Router } from "express";
const router: Router = Router();
router.get("/", getInfo);
// ...other routes...
export = router;

It's work for me....enjoy :)

Is this fixed ?

Was this page helpful?
0 / 5 - 0 ratings