错误TypeError:使用自定义TypeScript类时,roadHouse.isRunning不是函数

时间:2019-06-09 06:16:55

标签: typescript electron

我在自己的Electron项目中创建了一个自定义Typescript类,并且在调用方法时遇到错误。

班级:

export class RoadHouse extends Object {
   public status: string = "ready";
   private _output: string = "";
   readonly startTime = Date.now();
   private running: boolean = false;

   constructor(private _message: String = "", private _prompt: String = "") {
       //this.setStatus(_message);
   }

   handleError(message: RoadHouse | string): void {
       this.setStatus("Status.Failed");
       if (message) {
          // 
       }
   }

   isRunning(): boolean {
       return this.running; // breakpoint set here
   }

   setStatus(value: string) {
       this._output = value;
   }

   start():void {
       this.running = true;
   }

   stop():void {
       this.running = false;
   }
}

在main.ts中:

var roadHouse:RoadHouse = new RoadHouse("Hello");
console.log("Is running:" + roadHouse.isRunning());

错误:

  

(节点:18454)UnhandledPromiseRejectionWarning:TypeError:roadHouse.isRunning不是函数

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "strict": true,
    "esModuleInterop": true,
    "strictNullChecks":false
  }
}

构建任务:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build Typescript",
            "command": "tsc"
        }
    ]
}

Package.json:

{
  "name": "typescript-electron",
  "version": "1.0.0",
  "description": "Electron and Typescript",
  "main": "index.js",
  "scripts": {
    "test": "start electron ."
  },
  "devDependencies": {
    "electron": "^5.0.3"
  }
}

2 个答案:

答案 0 :(得分:0)

您可以在要调试的行中设置调试器,然后按F12键,但是代码可以正常工作。

https://stackblitz.com/edit/angular-zpd9bp

答案 1 :(得分:0)

该类正在扩展Object。当我删除它的工作。