reactjs - Cors TypeScript 中没有重载匹配此调用

标签 reactjs firebase express cors google-cloud-functions

我想使用 Express TypeScript 创建 API,但是当我想使用 cors 时,它显示如下错误:

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.

我使用 yarn add cors express 安装了该软件包和 yarn add @types/cors @types/express
这是我的代码:
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);

错误在 app.use(cors({ origin: true });

最佳答案

Specifying the path is optional for use ,以及 cors docs作为一个有效的例子,准确地展示你做了什么。 IOW,你的代码很棒!那里没有问题。

最近对 express 类型定义进行了更改。好像有两套:@types/express@types/express-serve-static-core .前者依赖后者,而there seems to be some breaking changesexpress-serve-static-core那个@types/express还没有完全 catch 。

在该线程中,提出了几种解决方法。

您可以固定 @types/express 的版本在您的 package.json到版本 4.17.0 .或者您可以使用 Typescript compiler's 禁用库检查skipLibCheck旗帜。

关于reactjs - Cors TypeScript 中没有重载匹配此调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59167258/

相关文章:

reactjs - 如何在react-redux中进行内存管理?

javascript - 在表单标签中打开一个默认输入

ios - 无法加载 tableview 单元格 firebase 的值

python - 导入错误 : cannot import name 'opentype'

node.js - Nodejs和express中post文件大小超出限制时如何响应客户端或浏览器

javascript - POST 正文未定义导致 Node.js 服务器关闭

reactjs - Heroku create-react-app deploy Uncaught SyntaxError : Unexpected token <

html - TypeScript:生成枚举值的类型或接口(interface)

angular - 更新 Firestore 中所有文档中的映射字段

javascript - Node.js (Openshift) 快速响应不起作用