tsconfig的useDefineForClassFields属性怎么使用
更新:HHH   时间:2023-1-7


这篇“tsconfig的useDefineForClassFields属性怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“tsconfig的useDefineForClassFields属性怎么使用”文章吧。

useDefineForClassFields 作用

class 声明中的字段语义从 [[Set]] 变更到 [[Define]]

示例

如下代码

export class C {
  foo = 100;
  bar: string;
}

useDefineForClassFields 设置为 false 时,编译后结果如下

export class C {
  constructor () {
    this.foo = 100;
  }
}

useDefineForClassFields 设置为 true 时,编译后结果如下, 要注意 target 版本要在 ES2021 之前的版本

export class C {
  constructor () {
    Object.defineProperty(this, 'foo', {
      enumerable: true,
      configurable: true,
      writable: true,
      value: 100
    })
    Object.defineProperty(this, 'bar', {
      enumerable: true,
      configurable: true,
      writable: true,
      value: void 0
    })
  }
}

target 版本在 ES2022 及其以上,编译结果如下

export class C {
  foo = 100;
  bar;
}

以上就是关于“tsconfig的useDefineForClassFields属性怎么使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注天达云行业资讯频道。

返回编程语言教程...