node.js - 打字稿错误:类扩展值未定义不是构造函数
问题描述
我正在使用一个抽象类来继承另一个类中的排序方法。
我收到以下错误:
抽象类“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
解决方案
推荐阅读
- powershell - 列出 VM 和分配的 GPU
- python-3.x - 为什么模拟的类型与模拟不匹配?
- ruby-on-rails - 如何在没有root用户的情况下在rails上为ruby运行docker容器?
- r - 如何使用 transition_states() 显示叠加图的过渡?
- c - 浮点值问题以及如何避免函数 f(x) 的无限值
- java - 在调用 oracle 数据库中的存储过程后,HikariCp 连接池无法重用会话
- javascript - 如何在页面刷新时重做方程?
- r - as_factor 函数的因子顺序异常
- azure-active-directory - 使用 msal.js 异步向租户权限和 Microsoft 个人帐户权限进行身份验证时出现 nonce_mismatch_error
- swift - 如何获得特定的日期格式?