Search code examples
node.jsexpress

no over load matches this call express typescript


When I try to add Response and Request to the parameter for the type interface I get error. I'm trying to create Node app with TypeScript addition. I was following some tutorial and basically did the same steps as he did yet he didn't receive this error. Did Node or TypeScript get some kind of updates that are causing this error?

No overload matches this call.
  The last overload gave the following error.
    Argument of type '(req: Request, res: Response) => Promise<void>' is not assignable to parameter of type 'Application<Record<string, any>>'.
      Type '(req: Request, res: Response) => Promise<void>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.ts(2769)
index.d.ts(164, 5): The last overload is declared here.

my res.status code error

    This expression is not callable.
  Type 'Number' has no call signatures.

here are the complete code

import express from "express";
    import NotesModel from "./models/notes";
    export const app = express();
    
    app.get("/", async (req: Request, res: Response) => {
      const notes = await NotesModel.find();
      res.status(200).json(notes) ;
    });

here is my version of express

"devDependencies": {
    "@eslint/js": "^9.12.0",
    "@eslint/migrate-config": "^1.3.1",
    "@types/express": "^5.0.0",
    "eslint": "^9.12.0",
    "globals": "^15.10.0",
    "nodemon": "^3.1.7",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "typescript-eslint": "^8.8.1"
  },

Solution

  • Older tutorials have not kept up to date with types from express which have become more strict over time. To fix your error, try to narrow down the res and req types like this:

    import express from "express";
    import NotesModel from "./models/notes";
    
    export const app = express();
        
    app.get("/", async (_req: express.Request, res: express.Response) => {
      const notes = await NotesModel.find();
      res.status(200).json(notes);
    });