使用 google api 库编译打字稿时出现“错误 TS2304:找不到名称‘Long’”

gae*_*123 2 google-api node.js typescript google-cloud-platform

我刚刚添加"@google-cloud/logging-winston":"2.1.0",到我的 pacakge.json 中,当我编译时出现以下错误。我偶尔会在其他 google 库中看到这种情况,其根本原因很可能在 protobuf 定义自动生成的类型的堆栈中更深。

../node_modules/@google-cloud/logging/build/proto/logging.d.ts:1434:32 - error TS2304: Cannot find name 'Long'.

1434                 line?: (number|Long|null);
                                    ~~~~

../node_modules/@google-cloud/logging/build/proto/logging.d.ts:1453:38 - error TS2304: Cannot find name 'Long'.

1453                 public line: (number|Long);
                                          ~~~~

../node_modules/@google-cloud/logging/build/proto/logging.d.ts:1543:39 - error TS2304: Cannot find name 'Long'.

1543                 requestSize?: (number|Long|null);
                                           ~~~~

../node_modules/@google-cloud/logging/build/proto/logging.d.ts:1549:40 - error TS2304: Cannot find name 'Long'.

1549                 responseSize?: (number|Long|null);
                                            ~~~~

../node_modules/@google-cloud/logging/build/proto/logging.d.ts:1576:42 - error TS2304: Cannot find name 'Long'.

1576                 cacheFillBytes?: (number|Long|null);

Run Code Online (Sandbox Code Playgroud)

gae*_*123 6

以下是我如何解决这个问题,直到它得到解决。

  1. 在您的 package.json 依赖项部分添加 "long":"4.0.0",
  2. 在您的 package.json devDependencies 部分:添加 "@types/long":"4.0.0",
  3. 最后,在 tsconfig.json (或在 tsc 命令行中)添加:
{
   "compilerOptions": {
     ...
     "types": [
       ...
       "long"
     ],
   ...
}
Run Code Online (Sandbox Code Playgroud)


小智 5

gae123 的建议在 typescript 版本 4.9.4 上对我不起作用。我将以下内容添加到 tsconfig.json。希望这可以帮助。

"compilerOptions":{
      ...
      "skipLibCheck": true,
}
Run Code Online (Sandbox Code Playgroud)