TS1086:不能在环境上下文中声明访问器

Ole*_*Ole 2 javascript typescript angular

当角运行,我得到thje错误TS1086: An accessor cannot be declared in an ambient context.的JavaScript getter和setter在这个抽象类打字稿

这是来源:


  /**
   * The current id key for the EStore instance.
   * @return this.config.idKey;
   */
  get ID_KEY(): string {
    return this.config.idKey
  }
  /**
   * The current guid key for the EStore instance.
   * @return this.config.guidKey;
   */
  get GUID_KEY(): string {
    return this.config.guidKey
  }
Run Code Online (Sandbox Code Playgroud)

在最新版本的 Angular 之前,这一直运行良好。我们不再允许在抽象类中使用 getter 和 setter 吗?

Rea*_*lar 18

这不是 Angular 错误。

TypeScript 3.7 对类型定义文件中的 getter 和 setter 进行了重大更改。

如上所述,TypeScript 3.7 在 .d.ts 文件中发出 get/set 访问器,这可能会导致消费者在旧版本的 TypeScript(如 3.5 及更早版本)上发生重大更改。TypeScript 3.6 用户不会受到影响,因为该版本已针对此功能进行了未来验证。

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#class-field-mitigations

如果您要使用使用 3.7 或更高版本生成的类型定义,则应使用 TypeScript 3.6 或更高版本。