0
votes

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.

1
I don't know if this answers your question, but someone else seemed to have a very similar problem that was caused by @types/node. Could you try updating that?Evan Hahn

1 Answers

1
votes

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