【问题标题】:Firebase CLI Typescript error TS2345 thrown on code that previously compiledFirebase CLI Typescript 错误 TS2345 在之前编译的代码上引发
【发布时间】:2020-11-07 20:46:16
【问题描述】:

晚上好,

我最近更新了用于云功能的 firebase CLI,当我尝试部署旧的 index.ts 文件时,出现 TS2345 错误:

src/index.ts:364:13 - error TS2345: Argument of type '(req: Request, res: Response<any>) => Response<any> | Promise<void | Response<any>>' is not assignable to parameter of type '(req: Request, resp: Response<any>) => void | Promise<void>'.
  Type 'Response<any> | Promise<void | Response<any>>' is not assignable to type 'void | Promise<void>'.
    Type 'Response<any>' is not assignable to type 'void | Promise<void>'.
      Type 'Response<any>' is not assignable to type 'Promise<void>'.

364  .onRequest((req, res) => {

引发此错误的函数如下所示:

exports.https_rec = functions.https
    .onRequest((req, res) => {
        if (req.method === 'PUT') {
            console.log("HTTPS Attempted Connection");
            return res.status(403).send('Forbidden!');
        }
        else{
           //Do Stuff
           return res.status(200).send("ok");
        }
    });

一切正常且上传正常,但在我更新 CLI 后,我现在在之前编译的代码上收到此 TS2345 错误。我从 Firebase 中找到了一个示例函数(几年前),其中的结构与我展示它的方式相同,这告诉我最近一定发生了一些变化。

https://github.com/firebase/functions-samples/blob/master/quickstarts/time-server/functions/index.js#L39

其他规格:
视窗 10
NPM 版本 14.5.0
Firebase-tools 版本 8.6.0

有什么想法或建议吗?是否可以轻松更改代码,或者我应该恢复到旧版本的 CLI 或 tslint?

提前感谢您的宝贵时间!

【问题讨论】:

    标签: node.js typescript firebase


    【解决方案1】:

    他们最近更改了 https 触发器的返回类型。

    你不应该“返回”任何东西作为回应。

    只需删除 return 即可。

    例子:

    exports.https_rec = functions.https
    .onRequest((req, res) => {
        if (req.method === 'PUT') {
            console.log("HTTPS Attempted Connection");
            return res.status(403).send('Forbidden!');
        }
        else{
           //Do Stuff
           res.status(200).send("ok");
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 2017-03-29
      相关资源
      最近更新 更多