helmet: Helmet + Express + Typescript = No overload matches this call error

Issue similar to: #324

My versions:

helmet@4.6.0
express@4.17.1
@types/express@4.17.13
@types/node@16.3.0
typescript@4.3.5
node@16.4.2

My code:

import express from "express";
import helmet from "helmet";
import cors from "cors";

const app = express();

app.use(helmet());
app.use(cors());
app.use(express.json());

tsconfig

{
  "compilerOptions": {
    "lib": ["es2020"],
    "module": "commonjs",
    "target": "es2020",

    "strict": true,
    "esModuleInterop": true,
    "sourceMap": true,
    "outDir": "dist"
  },
  "include": ["**/*.ts", "./client/**/*"]
}

Error:

No overload matches this call. The last overload gave the following error. Argument of type ‘(req: IncomingMessage, res: ServerResponse, next: (err?: unknown) => void) => void’ is not assignable to parameter of type ‘PathParams’. Type ‘(req: IncomingMessage, res: ServerResponse, next: (err?: unknown) => void) => void’ is missing the following properties from type ‘(string | RegExp)[]’: pop, push, concat, join, and 27 more.ts(2769)

I was able to workaround by making this change: app.use(helmet() as express.RequestHandler);

I have the same issue with the app.use(express.json());, so not sure if this is related with helmet at all.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (6 by maintainers)

Most upvoted comments

I downgraded some packages and found out that the issue is with @types/node. I was using the latest version, v16.3.1, and when I downgraded to v14.17.4 the errors disappeared.

EDIT: After further investigation, the errors start to show up when using @types/node v15.9.0 or higher.

15.9.x still gives me errors on bodyParser.json() and helmet() 15.6.x is 👌 for me

Syntax wise I found that these packages are decent for helmet

import helmet from ‘helmet’; const app = express(); app.use(helmet());

"@types/cookie-session": "^2.0.43",
"@types/express": "^4.17.13",
"@types/helmet": "^4.0.0",
"@types/jsonwebtoken": "^8.5.4",
"@types/mongoose": "^5.11.97",
"cookie-session": "^1.4.0",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"express-validator": "^6.12.1",
"helmet": "^4.6.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.13.7",
"typescript": "^4.3.5"

at least compiler is ok with it