“错误TS2304:使用Google api库编译打字稿时找不到名称'Long'”
我刚刚在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);
回答如下:在解决此问题之前,我是如何解决此问题的。
- 在您的package.json依赖项部分中,添加
"long":"4.0.0",
- 在您的package.json devDependDependence部分:添加
"@types/long":"4.0.0",
- 最后,在tsconfig.json中(或在tsc命令行中添加:
{
"compilerOptions": {
...
"types": [
...
"long"
],
...
}