错误:在Thrift中使用Typescript

时间:2018-12-13 06:12:18

标签: typescript thrift typescript-typings

我想在我的Node.js项目中使用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/**/*"]
}

似乎还好吧?仍然遇到错误。知道如何成功构建它吗?

P.S。我正在使用Typescript 3.2,Thrift v0.11和@ types / thrift 0.10.7

1 个答案:

答案 0 :(得分:0)

安装类型后,仍然需要先导入它们,然后再引用它们。

编辑:

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