如何为Babel设置可枚举属性

3
我正在尝试在 TypeScript 项目中使用用 Babel 编译的模块。但是在静态属性和方法方面出现了错误。
这是 TypeScript 中 extends 的帮助器。你可以看到它使用了 for..in
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};

有没有办法为Babel的静态属性/方法设置enumerable属性? enter image description here

你可以手动设置 Object.defineProperty(XmlObject, 'LoadXml', {enumerable: true})。ES6规范规定它们是不可枚举的。 - loganfsmyth
2
你说你遇到了一个错误,但没有说明是什么错误。根据ES6规范(参见14.5.14运行时语义:ClassDefinitionEvaluation下的21.b),静态方法不可枚举。你的截图显示Test.LoadXml()正确调用了XmlObject.LoadXml(它记录了“Success”)。你想要实现什么? - Jordan Running
@Jordan,我在Test.LoadXml()上没有结果。这是一个错误,因为它必须有这个函数。 - Microshine
1
在我看来,应该修复TypeScript助手。 - Felix Kling
1个回答

0

ES2015类转换插件中有一个宽松选项,其默认值为false。您可以像这样将该值设置为true

{
  "plugins": [
    ["transform-es2015-classes", {
      "loose": true
    }]
  ]
}

使用此设置,方法将在类原型对象上定义。但这会引起其他一些问题。

class Foo {
  set bar() {
    throw new Error("foo!");
  }
}
class Bar extends Foo {
  bar() {
    // will throw an error when this method is defined
  }
}

请查看文档以获取更多详细信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接