【问题标题】:How do I force TypeScript to transpile prototype methods?如何强制 TypeScript 转换原型方法?
【发布时间】:2018-08-09 18:14:52
【问题描述】:

我正在尝试在 TypeScript 中定义一个类的方法,然后填充该类,但是当我尝试调用这些方法时,它说它们不存在。

打字稿:

export class ColumnHeader extends JSONObject {
    // ...     
    isCountrySubtype(): boolean {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == ColumnSubtype.Country.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new CountryType()).class);
    }

    isRegionSubtype(): boolean {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == ColumnSubtype.Region.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new RegionType()).class);
    }

    isDmaSubtype(): boolean {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == ColumnSubtype.Dma.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new DmaType()).class);
    }

    isCitySubtype(): boolean {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == ColumnSubtype.City.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new CityType()).class);
    }

    isPostalCodeSubtpe(): boolean {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == ColumnSubtype.PostalCode.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new PostalCodeType()).class);
    }
    // ...
}

转译的 JavaScript:

class ColumnHeader extends JSONObject {
    constructor() {

    }
    isCountrySubtype() {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == enums_1.ColumnSubtype.Country.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new CountryType()).class);
    }
    isRegionSubtype() {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == enums_1.ColumnSubtype.Region.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new RegionType()).class);
    }
    isDmaSubtype() {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == enums_1.ColumnSubtype.Dma.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new DmaType()).class);
    }
    isCitySubtype() {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == enums_1.ColumnSubtype.City.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new CityType()).class);
    }
    isPostalCodeSubtpe() {
        return (this.columnSubtype && this.columnSubtype.toLowerCase() == enums_1.ColumnSubtype.PostalCode.toLowerCase()) ? true : (this.type != null && this.type.class != null && this.type.class == (new PostalCodeType()).class);
    }
}

我可能需要原型语法,以便与尚不能使用 ES2015 的部分实现跨系统兼容性。

【问题讨论】:

  • 你设置--targetcompiler option了吗?你要输出 ES5 吗?
  • 嗨@jcalz,对不起,我在 Scrum 会议开始之前过早地发布了我的问题。我添加了实际用于填充对象的代码,因为这可能是有问题的代码。
  • isRegionType 应该来自哪里?因为它不在您在问题中定义的 ColumnHeader
  • 安装 extend,在 ColumnHeader 中定义 isRegionType 都可以使用您提供的代码按预期工作
  • 对不起@TitianCernicova-Dragomir,我试图发布一个极简主义的例子,因为我不想用本质上相同的样板方法向我的问题发送垃圾邮件 5 次,并且我复制了错误的代码。让我试试别的。

标签: json node.js typescript


【解决方案1】:

您将target 设置为 ES2015(或更高版本)。这就是 Javascript 包含类的原因。 ES2015 支持类,所以不需要使用prototype。您可以定位 es5 并使用旧的方式在 Javascript 中进行类。您可以在 tsconfig.json 或 Visual Studio 或 tsc 命令行中设置它(取决于您如何设置 typescript)。对于tsconfig.json

{
    "compilerOptions": {
        "target": "es6"
    }
}

但是,您的错误似乎与此无关,可能在运行时 columnHeader 不是预期的类型。

【讨论】:

  • 好像是这样。我在 Scrum 会议开始前 3 秒发布了我的问题,却忘记发布我用来填充对象的代码。我在想我要离开 NPM 的 extend 函数可能正在从对象中删除这些函数。
猜你喜欢
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
  • 2020-11-16
  • 1970-01-01
相关资源
最近更新 更多