【问题标题】:Properties and methods on exported typescript classes don't exist (TS 2339)导出的 typescript 类的属性和方法不存在 (TS 2339)
【发布时间】:2019-05-03 21:02:18
【问题描述】:

我有一个模块,它是帮助类的集合,每个类都定义在自己的文件中:controller.tsstateService.ts 等等。从我收集到的将它们全部导出的方法是创建一个 index.ts 来导入所有它们然后导出它们(作为模块的接口)。

总的来说,我的文件看起来像这样:

export default class Controller {
    public state: stateService; // This will become relevant in a moment

    // other things in the class
}

我的index.ts 是这些列表:

export { default as Controller } from './controller';

我的tsconfig.json是这样的:

{
    "compilerOptions": {
        "moduleResolution": "node",
        "noImplicitAny": true,
        "preserveConstEnums": true,
        "outDir": "dist",
        "sourceMap": true,
        "noEmitOnError": true,
        "target": "es6",
        "module": "commonjs",
        "declaration": true,
        "experimentalDecorators": true,
        "suppressImplicitAnyIndexErrors": true
    },
    "include": ["src/**/*"],
    "exclude": ["node_modules", "tests", "src/**/*.spec.ts"],
    "types": ["jest"]
}

现在我有一个示例项目可以使用 npm link 在本地测试模块:

export default class OwnerController extends Controller {
    foo() {
        this.state.get(bar);
    }
}

当我尝试在依赖项目中使用我的Controller 类并访问state 属性时出现以下错误:

[ts] Property 'state' does not exist on type 'OwnerController'. [2339]

以同样的方式,类方法也会出现此错误:

[ts] Property 'start' does not exist on type 'CarExampleApp'. [2339]

谁能告诉我我做错了什么? (谢谢!)

【问题讨论】:

  • OwnerController 对象是什么?是不是类似于class OwnerController extends Controller { ... }
  • 是的,我现在更新上面的内容
  • 我认为您描述的代码没有任何问题。您是在链接已编译的模块还是在模块的根目录中直接导入 index.ts 文件。如果您使用的是编译变体,您是否检查了controller.d.ts 中的编译定义?也许你会在那里找到一些东西
  • 我编译它并在 package.json 中指定 ./dist/index.js 是模块的入口点。
  • controller.d.ts 内我有这个:` export default class Controller { state: StateService; // 其他东西 }` 所以对我来说它看起来不错,在index.d.ts 里面我只有 export { default as Controller } from './controller'; controller.d.ts 中还有另一个类带有 declare 关键字,我应该使用 declare 还是 module?我假设即使有 controller.d.ts 我仍然需要导入 index.d.ts 才能显示它?

标签: javascript typescript types node-modules


【解决方案1】:

我最终发现了这个问题,@Aravindan 指出,确保在构建后重新运行 npm link,以检查它是否始终处于最新版本。

对我来说,问题是依赖项目的名称与它在 package.json 中所依赖的项目相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-10
    • 2019-08-11
    • 2021-08-02
    • 2020-06-02
    • 1970-01-01
    • 2022-07-09
    • 2022-10-04
    • 2023-02-07
    相关资源
    最近更新 更多