首页 > 解决方案 > TS1086:无法在环境上下文中声明访问器

问题描述

使用 Angular 运行时,我在此 Abstract Typescript 类中遇到 Javascript getter 和 setter错误TS1086: An accessor cannot be declared in an ambient context.

这是来源:


  /**
   * 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
  }

在最新版本的 Angular 之前,这一切正常。我们是否不再允许在抽象类中使用 getter 和 setter?

标签: javascriptangulartypescript

解决方案


这不是 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 或更高版本。


推荐阅读