升级到 ember-typescript 3 抱怨默认参数

Upgrade to ember-typescript 3 complains about default parameters

我们的 Ember 组件经常使用以下模式,该模式与 ember-typescript 2 一起正常工作:

export default class DatePicker extends Component {
  format: string = this.format || 'dd/LL/yyyy';
}

如果格式未通过模板传递,则使用 'dd/LL/yyyy' 对其进行初始化。

对于 ember-typescript 3 和更新的 TS 版本,编译器不再满意。它抱怨

error TS2729: Property 'format' is used before its initialization.

因为它对 this.format 的用法不满意。

我应该怎么做才能解决这个问题?我们的代码(到目前为止工作正常)是否无效并且应该更改?如果是怎么办?

您不需要将 this.format 作为默认设置的一部分。只是做:

export default class DatePicker extends Component {
  format: string = 'dd/LL/yyyy';
}

如果您的模板传入 @format,它将覆盖组件 TS 文件中的默认值。