使用Google api库编译打字稿时出现“错误TS2304:找不到名称“长””

时间:2019-09-29 20:53:37

标签: node.js typescript google-cloud-platform google-api

我刚刚在pacakge.json中添加了"@google-cloud/logging-winston":"2.1.0",,并且在编译时出现以下错误。我偶尔在其他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);

1 个答案:

答案 0 :(得分:2)

在解决此问题之前,这是我的解决方法。

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