Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No overload matches this call in Cors TypeScript

I want to create API using Express TypeScript, but when I want to use cors it shown error like this:

No overload matches this call.
The last overload gave the following error.
Argument of type 'RequestHandler<any>' is not assignable to parameter of type 'PathParams'.
Type 'RequestHandler<any>' is missing the following properties from type '(string | RegExp)[]': length, pop, push, concat, and 26 more.

I installed the package using yarn add cors express and yarn add @types/cors @types/express

This is my code:

const api = express();
const main = express();

api.use(cors({ origin: true }));

main.use(bodyParser.json());
main.use(bodyParser.urlencoded({ extended: false }));
main.use("/api/v1", api);

export const webApi = functions.https.onRequest(main);

The error is in app.use(cors({ origin: true });

like image 869
Wahyu Kurniawan Avatar asked Dec 04 '19 00:12

Wahyu Kurniawan


1 Answers

Specifying the path is optional for use, and the cors docs show exactly what you did as a valid example. IOW, your code is great! There's no problem there.

There was recently a change to the express type definitions. It seems like there are two sets: @types/express and @types/express-serve-static-core. The former depends on the latter, and there seems to be some breaking changes in express-serve-static-core that @types/express hasn't quite caught up with yet.

In that thread, a couple of workarounds are proposed.

You can pin the version of @types/express in your package.json to version 4.17.0. Or you can disable library checking with the Typescript compiler's skipLibCheck flag.

like image 133
M Falanga Avatar answered Oct 02 '22 13:10

M Falanga