#ExpressJS TypeScript "No overload matches this call"

3 messages · Page 1 of 1 (latest)

jagged pumice
#

Hi Guys,

This is probably asked lots but I was wondering how you fix this issue?

I have the code:

app.get('/test', (req: Request, res: Response) => {
    console.log(req.url);
    return res.sendStatus(200);
})

The 2nd parameter's parameters (the call back function parameters) are underlined with the error:

  The last overload gave the following error.
    Argument of type '(req: Request, res: Response) => express.Response<any, Record<string, any>>' 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<...>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.```

I was wondering how to fix this? I really have no clue why it is happening so some help would be appreciated. 
I am migrating a project from JS to TS and so there is still some other JS code however I am doing the following:
```ts
import express, { Express, Request, Response, NextFunction } from 'express';

And I have installed the @types/express and @types/node packages.

I am also defining the app function correctly I believe so there shouldn't be an issue with that?

const parsedPort: number = process.env.PORT ? parseInt(process.env.PORT) : NaN;
const PORT: number = isNaN(parsedPort) ? 3000 : parsedPort;

const app: Express = express();
https.createServer({ key, cert }, app).listen(PORT, () => {
    console.log(`Listening on port ${PORT}`);
});

Many thanks for any help provided.

#

The entire index.ts file is as follows:

import express, { Express, Request, Response } from 'express';
import https from 'https';
import fs from 'fs';

const parsedPort: number = process.env.PORT ? parseInt(process.env.PORT) : NaN;
const PORT: number = isNaN(parsedPort) ? 3000 : parsedPort;

const key = fs.readFileSync("localhost-key.pem", "utf-8");
const cert = fs.readFileSync("localhost.pem", "utf-8");

const app: Express = express();
https.createServer({ key, cert }, app).listen(PORT, () => {
    console.log(`Listening on port ${PORT}`);
});


app.get('/test', (req: Request, res: Response) => {
    console.log(req.url);
    return res.sendStatus(200);
})

#

And I still get the error with just:

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

const app: Express = express();

app.get('/test', (req: Request, res: Response) => {
    console.log(req.url);
    return res.sendStatus(200);
})

(without even listening for the requests)