Firebase 函数初始 Hello World 无法在 TS 中编译

Avi*_*ion 3 firebase typescript firebase-tools google-cloud-functions grpc-node

我有一个托管在 Firebase 中的现有 webapp 项目,我想向其中添加云功能。但是,当我仅使用该命令运行时,firebase init functions由于 gRPC 节点模块中的错误,它生成的初始 hello-world 模板无法在 TypeScript 中编译。

\n

我的文件夹结构:

\n
firebase\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .firebaserc\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 firebase.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 webapp     // My web app. Compiles and runs well.\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tsconfig.json\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 functions  // Only what was autogenerated by firebase-tools.\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .eslintrc.js\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tsconfig.json\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tsconfig.dev.json\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.ts\n
Run Code Online (Sandbox Code Playgroud)\n

我做了什么

\n

在我的firebase根文件夹中,我运行firebase init functions并选择了正确的项目,使用 TypeScript,具有默认的 ESLint 定义,并安装软件包。

\n

我注释掉了 中的 Hello World 函数functions/src/index.ts

\n

我正在使用最新的 Node 16 LTS 和 firebase-tools 版本 10.0.1。

\n

我遇到的错误

\n

当我npm run build在我的functions文件夹中运行时,我收到这些错误。

\n
> build\n> tsc\n\nnode_modules/@grpc/grpc-js/build/src/object-stream.d.ts:11:18 - error TS2430: Interface 'IntermediateObjectWritable<T>' incorrectly extends interface 'Writable'.\n  The types returned by 'end(...)' are incompatible between these types.\n    Type 'void' is not assignable to type 'this'.\n      'this' could be instantiated with an arbitrary type which could be unrelated to 'void'.\n\n11 export interface IntermediateObjectWritable<T> extends Writable {\n                    ~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nnode_modules/@grpc/grpc-js/build/src/server-call.d.ts:64:5 - error TS2416: Property 'end' in type 'ServerWritableStreamImpl<RequestType, ResponseType>' is not assignable to the same property in base type 'Writable'.\n  Type '(metadata?: any) => void' is not assignable to type '{ (cb?: (() => void) | undefined): this; (chunk: any, cb?: (() => void) | undefined): this; (chunk: any, encoding: BufferEncoding, cb?: (() => void) | undefined): this; }'.\n    Type 'void' is not assignable to type 'this'.\n      'this' could be instantiated with an arbitrary type which could be unrelated to 'void'.\n\n64     end(metadata?: any): void;\n       ~~~\n\nnode_modules/@grpc/grpc-js/build/src/server-call.d.ts:77:5 - error TS2416: Property 'end' in type 'ServerDuplexStreamImpl<RequestType, ResponseType>' is not assignable to the same property in base type 'Duplex'.\n  Type '(metadata?: any) => void' is not assignable to type '{ (cb?: (() => void) | undefined): this; (chunk: any, cb?: (() => void) | undefined): this; (chunk: any, encoding?: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "base64url" | "latin1" | "binary" | "hex" | undefined, cb?: (() => void) | undefined): this; }'.\n    Type 'void' is not assignable to type 'this'.\n      'this' could be instantiated with an arbitrary type which could be unrelated to 'void'.\n\n77     end(metadata?: any): void;\n       ~~~\n\n\nFound 3 errors.\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试过的

\n
    \n
  • 尝试使用最新的节点 14 LTS 恢复到 firebase-tools 版本 9.23.3。同样的错误。
  • \n
  • 从头开始尝试了一个新的 firebase 项目。遇到同样的错误。
  • \n
  • 尝试从我的 M1 Mac 恢复到之前的 Intel Mac。也没有帮助。
  • \n
\n

唯一对我有用的是使用 JS firebase-functions 模板而不是 TS 模板,看起来这就是我现在将继续进行的操作。因为它一开始就工作得很好,而且我部署它时没有出现任何问题,而且工作得很好。但我真的更喜欢使用 TS。

\n

我确实有一种感觉,我在这里错过了一些非常基本的东西。任何帮助将非常感激。谢谢 :)

\n

sam*_*man 7

更新:2022 年 1 月 6 日

已经发布了新版本@grpc/grpc-js,现在通过@murgatroid99解决了这个问题。只需安装 v1.4.6 或更高版本并按照原始问题线程中的说明更新所有依赖项即可。

npm install --save @grpc/grpc-js@^1.4.6
Run Code Online (Sandbox Code Playgroud)

这是一个已知的兼容性问题,@types/nodeIssue #2002@grpc/grpc-js中所讨论。

当前的解决方案是将您的@types/node依赖项降级回一个版本。

直到它被修补:

  • 如果您有 v14.18.4,请返回到 v14.18.3。
  • 如果您有 v17.0.6,请返回 v17.0.5。
  • 等等...

您将其添加到您的package.json文件中:

"@types/node": "14.18.3"
Run Code Online (Sandbox Code Playgroud)