首页 > 解决方案 > 打字稿错误:类扩展值未定义不是构造函数

问题描述

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

我收到以下错误:

抽象类“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);
        }
      }
    }
  }
}

子类 NumbersCollection.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

标签: node.jstypescripttypescript-typings

解决方案


推荐阅读