打字稿错误:未定义的类扩展值不是构造函数

时间:2021-06-22 15:17:00

标签: node.js typescript typescript-typings

我正在使用一个抽象类来继承另一个类中的 sort 方法。

我收到以下错误:

抽象类“index.ts”

export abstract class Sorter {
  abstract length: number;
  abstract swap(leftIndex: number, rightIndex: number): void;
  abstract compare(leftIndex: number, rightIndex: number): boolean;
  sort(): void {
    const length = this.length;
    for (let i = length - 1; i >= 0; i--) {
      for (let j = 0; j < i; j++) {
        if (this.compare(j, j + 1)) {
          this.swap(j, j + 1);
        }
      }
    }
  }
}

子类编号Collection.ts

import { Sorter } from './index';

// class to sort the numbers collection
export class NumbersCollection extends Sorter {
  data: number[];
  get length() {
    return this.data.length;
  }
  constructor(data: number[]) {
    super();
    this.data = data;
  }
  compare(leftIndex: number, rightIndex: number): boolean {
    return this.data[leftIndex] > this.data[rightIndex];
  }
  swap(leftIndex: number, rightIndex: number): void {
    let tmp = this.data[leftIndex];
    this.data[leftIndex] = this.data[rightIndex];
    this.data[rightIndex] = tmp;
  }
}

错误信息

[start:run] C:\Users\illum\Work\Study\Typescript\sort\build\NumbersCollection.js:11
[start:run]             
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
[start:run]             ^
[start:run]
[start:run] TypeError: Class extends value undefined is not a constructor or null

0 个答案:

没有答案