我想使用 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 changes在 express-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/