【问题标题】:extends and implements in one class in TypeScript在 TypeScript 中的一个类中扩展和实现
【发布时间】:2015-06-02 21:28:57
【问题描述】:

我可以在 TypeScript 中执行此操作吗?

export interface IMyInterface {
  doSomething(): void;
}

export class MyBaseClass {
  myBaseClassHasProperty: string;

  constructor(){
    this.myBaseClassHasProperty = 'some value';
  }
  myBaseClassHasMethods(): void {
    console.log(this.myBaseClassHasProperty);
  }
}

export class MyClass extends MyBaseClass implements IMyInterface {
  constructor() {
    super();
  }

  doSomething(): void {
    this.myBaseClassHasMethods();
  }
}

在运行时它抛出:

Uncaught ReferenceError: MyBaseClass is not defined

【问题讨论】:

    标签: typescript extends implements


    【解决方案1】:

    在运行时我得到这个Uncaught ReferenceError: MyBaseClass is not defined

    是的,您可以这样做。您发布的代码可以正常工作。

    但我怀疑在您的实际代码中,您将其拆分为多个文件,并且MyBaseClassMyClass 的代码之前未执行

    修复 JavaScript 排序或使用外部模块让模块加载器确定排序。

    【讨论】:

    • 是的,我正在使用 tsconfig.json 仅编译为一个 app.js 文件,而 tsc 正在创建一个 MyBaseClass.js 文件。但仍然无法正常工作,tsc 没有在 js 文件上创建 __extends 代码,我稍后会尝试更多。 - Mac OS X 10.8 上的 tsc --version 1.4.1.0
    猜你喜欢
    • 2014-04-21
    • 2022-01-15
    • 2016-06-29
    • 2015-01-25
    • 2016-10-21
    • 1970-01-01
    • 2016-12-14
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多