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

Firebase Functions Initial Hello World Doesn't Compile in TS

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

我的文件夹结构:

firebase
├── .firebaserc
├── firebase.json
├── webapp     // My web app. Compiles and runs well.
│   ├── package.json
│   ├── tsconfig.json
│   └── src
└── functions  // Only what was autogenerated by firebase-tools.
    ├── .eslintrc.js
    ├── package.json
    ├── tsconfig.json
    ├── tsconfig.dev.json
    └── src
        └── index.ts

我做了什么

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

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

我使用的是最新的 node 16 LTS 和 firebase-tools 版本 10.0.1。

我遇到的错误

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

> build
> tsc

node_modules/@grpc/grpc-js/build/src/object-stream.d.ts:11:18 - error TS2430: Interface 'IntermediateObjectWritable<T>' incorrectly extends interface 'Writable'.
  The types returned by 'end(...)' are incompatible between these types.
    Type 'void' is not assignable to type 'this'.
      'this' could be instantiated with an arbitrary type which could be unrelated to 'void'.

11 export interface IntermediateObjectWritable<T> extends Writable {
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~

node_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'.
  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; }'.
    Type 'void' is not assignable to type 'this'.
      'this' could be instantiated with an arbitrary type which could be unrelated to 'void'.

64     end(metadata?: any): void;
       ~~~

node_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'.
  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; }'.
    Type 'void' is not assignable to type 'this'.
      'this' could be instantiated with an arbitrary type which could be unrelated to 'void'.

77     end(metadata?: any): void;
       ~~~


Found 3 errors.

我试过的

唯一对我有用的是使用 JS firebase-functions 模板而不是 TS 模板,这似乎是我现在要继续进行的。因为它一开始就很好用,而且我部署它时没有打嗝,而且效果很好。但是我真的更喜欢用TS。

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

更新:2022 年 1 月 6 日

@grpc/grpc-js 的新版本已经发布,现在修复了这个问题,感谢 @murgatroid99. Just install v1.4.6 or later and update any dependencies as instructed in the original issue 的帖子。

npm install --save @grpc/grpc-js@^1.4.6

这是 @types/node@grpc/grpc-js 之间的已知兼容性问题,如 Issue #2002 中所述。

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

直到它被修补:

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

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

"@types/node": "14.18.3"

这是 @types/node@grpc/grpc-js 之间的兼容性问题,已在 @grpc/grpc-js 的 1.4.6 版中修复。如果您更新依赖项以选择该版本,则不会再出现此错误。