typescript - 错误:将 Thrift 与 Typescript 一起使用时找不到名称“Thrift”
问题描述
我想在我的 Nodejs 项目中使用 Thrift 和 Typescript 这是我遇到的错误
src/gen-js/myService.d.ts:9:12 - error TS2503: Cannot find namespace 'Thrift'.
10 input: Thrift.TJSONProtocol;
~~~~~~
src/gen-js/myService.d.ts:10:1 - error TS2503: Cannot find namespace 'Thrift'.
11 output: Thrift.TJSONProtocol;
~~~~~~
error TS2304: Cannot find name 'Thrift'.
...(more of the same)
src/gen-js/myService_types.d.ts:198:32 - error TS2304: Cannot find name 'Thrift'.
198 class StoreException extends Thrift.TException {
~~~~~~
...(more of the same)
我已经安装@types/thrift
并设置了我的 tsconfig.json 如下
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es2017",
"noImplicitAny": true,
"moduleResolution": "node",
"inlineSourceMap": true,
"outDir": "dist",
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/types/*"]
},
"lib": [
"dom",
"esnext"
]
},
"include": ["src/**/*"]
}
看起来不错吧?并且仍然遇到错误。知道如何成功构建它吗?
PS 我正在使用 Typescript 3.2、Thrift v0.11 和 @types/thrift 0.10.7
解决方案
安装类型后,您仍然需要在引用它们之前导入它们。
编辑:
查看类型定义后,它不是.. 理想的类型。如果使用通配符导入类型(第二个示例),还有一个嵌套Thrift
命名空间
import { Thrift, TJSONProtocol } from 'thrift';
const protocol: TJSONProtocol = // instance;
class StoreException extends Thrift.TException {}
或者
import * as Thrift from 'thrift';
const protocol: Thrift.TJSONProtocol = // instance;
class StoreException extends Thrift.Thrift.TException {}
推荐阅读
- unity3d - Raycast如何处理同一层的多个对象
- mysql - ReadUncommitted 可以知道所有将存在的 LOWER 自动增量 ID 吗?
- json - Gson - 具有空值的 JsonObject
- json - 如何使用 React 延迟加载导入 Json 文件?
- machine-learning - 如何将事件响应建模到分类器的数据集中
- vba - 按主题和日期范围选择新电子邮件
- python - 消息:无法定位元素?
- python - 当只提到 name[0].lower() == "r" 时,为什么这个函数也适用于 'R'?
- javascript - es2016 在类构造函数中处理回调
- javascript - create-react-app 安装报告了 NPM 漏洞,我应该做些什么吗?