【问题标题】:Error: Cannot find name 'Thrift' when using Thrift with Typescript错误:将 Thrift 与 Typescript 一起使用时找不到名称“Thrift”
【发布时间】:2018-12-13 14:47:42
【问题描述】:

我想在我的 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/**/*"]
}

看起来不错吧?并且仍然遇到错误。知道如何成功构建它吗?

附:我正在使用 Typescript 3.2、Thrift v0.11 和 @types/thrift 0.10.7

【问题讨论】:

    标签: typescript thrift typescript-typings


    【解决方案1】:

    在安装类型之后,您仍然需要在引用它们之前导入它们。

    编辑:

    查看类型定义后,它不是……理想的类型。如果使用通配符导入类型(第二个示例)还有一个嵌套的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 {}
    

    【讨论】:

    • 我同意,打字根本不理想。 vscode 抛出的另一个错误是 [2306],这意味着 myService.d.ts is not a module。另外,我确实忘记提及生成的文件中会引发错误。我马上修改。
    • @Ashniu123,确保你已经在.ts 文件中导入了thrift 模块,清理构建目录并再次构建。我刚刚建立了一个快速测试项目,它编译并运行得很好。
    • 我在这里创建了一个简单的要点,请解释一下有什么问题gist.github.com/Ashniu123/c73f81b0151c27289779a8a632cbc631
    • @Ashniu123,据我所知,您现在遇到的错误与原来的错误完全不同。那是什么?
    • 错误还是一样,我使用的 thrift 文件太长(200+ 行)所以我用最少的服务功能做了一个较短的版本。但是,如果您能解决 ping() 问题,我将不胜感激。我针对的错误是生成的gen-js 文件夹中的error TS2503: Cannot find namespace 'Thrift'.。应该不会来吧?可能是打字稿版本问题吗?我使用的是 3.2,而我认为生成的文件更符合 2.3。
    猜你喜欢
    • 2020-09-20
    • 2012-10-04
    • 2017-07-31
    • 2020-06-30
    • 2017-10-30
    • 2014-04-20
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多