在 TypeScript 中获取 Class Class 子类数组的名称?

Get Class Name of Class Subclassing Array in TypeScript?

我在 TypeScript 中有以下 class(从 JavaScript 移植,其中 [在添加类型之前]):

class A extends Array {
    private _foo: string;

    constructor(settings: any) {
        super();
        this._foo = 'foo';
    }

    get Foo() {
        return (this._foo);
    }
    set Foo(value) {
        this._foo = value;
    }
}
let test = new A({ });
test.push('1');
test.push(2);
test.push('3');

// outputs "A" in JavaScript
// outputs "Array" in TypeScript
console.log(test.constructor.name);

代码段底部概述了该问题,我希望从“A”的 test.constructor.name 中取回 class 名称,但我得到的是“Array”。似乎在 subclassing Array 对象的特定情况下,TypeScript 吹走了构造函数名称。有没有办法在不依赖 .constructor.name 的 TypeScript 中找到 class 名称?

我已经在 Playground here

中测试了您的代码

并且它正确地输出了“A”。我怀疑发生了其他事情,可能与您的构建工具或配置有关。

我建议尝试仅使用 tsc 来缩小问题范围。如果 tsc 正确输出,您可以继续使用任何其他插件,或构建步骤

发生这种情况是因为您在 compilerOptions 和 Typescript polyfills class

中将 es5 作为目标

查看为 es5 vs ES2015 生成的代码。

如果您不需要支持非常旧的浏览器,您可以安全地使用比 es5

更高的目标