0

My Express server has the following 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());

and I'm getting errors on both the helmet() and express.json() calls:

helmet(): 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)

express.json(): No overload matches this call. The last overload gave the following error. Argument of type 'NextHandleFunction' is not assignable to parameter of type 'PathParams'. Type 'NextHandleFunction' is missing the following properties from type '(string | RegExp)[]': pop, push, concat, join, and 27 more.ts(2769)

I'm using Express 4.17.1, TypeScript 4.3.5, and Helmet 4.6.0.

I always used helmet and express.json like this, but recently I started seeing these typescript errors.

Does anyone know how to fix these?

Thank you.

Carlos
  • 25
  • 5
  • I don't know if this answers your question, but [someone else seemed to have a very similar problem](https://github.com/helmetjs/helmet/issues/324) that was caused by `@types/node`. Could you try updating that? – Evan Hahn Jul 08 '21 at 16:14

1 Answers1

4

I also had the same problem, you can cast it to the correct type:

app.use(helmet() as express.RequestHandler);

Or you can do something like this if you don't like casting

// from https://github.com/DefinitelyTyped/DefinitelyTyped/issues/43909
app.use((req, res, next) => { next(); }, cors()); // change cors() to what you want 
xhg
  • 1,850
  • 2
  • 21
  • 35
  • This workaround works, thanks! Although this seems to be an issue with helmet and/or @types/express. – Carlos Jul 09 '21 at 11:05