最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TS1086: An accessor cannot be declared in an ambient context - Stack Overflow

programmeradmin4浏览0评论

When running with Angular, I'm getting thje error TS1086: An accessor cannot be declared in an ambient context. for Javascript getters and setters in this Abstract Typescript class.

This is the source:


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

This was working fine until the most recent version of Angular. Are we no longer allowed to use getters and setters in Abstract classes?

When running with Angular, I'm getting thje error TS1086: An accessor cannot be declared in an ambient context. for Javascript getters and setters in this Abstract Typescript class.

This is the source:


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

This was working fine until the most recent version of Angular. Are we no longer allowed to use getters and setters in Abstract classes?

Share Improve this question asked Dec 27, 2019 at 14:27 OleOle 46.9k68 gold badges237 silver badges441 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

This isn't an Angular error.

TypeScript 3.7 introduced a breaking change for getters and setters in the type definition files.

As mentioned above, TypeScript 3.7 emits get/set accessors in .d.ts files which can cause breaking changes for consumers on older versions of TypeScript like 3.5 and prior. TypeScript 3.6 users will not be impacted, since that version was future-proofed for this feature.

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

You should be using TypeScript 3.6 or higher if you're going to consume type definitions generated with 3.7 or higher.

I had this a similar problem with google-auth-library.

I'm on Ubuntu and call tsc to transpile my nodejs server from typescript to javascript, but running 'tsc' was throwing the 'TS1086: An accessor cannot be declared in an ambient context' error.

Initially, I tried updating typescript by running npm install typescript@latest from my server directory but was still seeing errors.

I had to update my global version of typescript so 'tsc' could run, but running npm install typescript@latest -g was also throwing errors. I had to remove the tsc link in /usr/bin/.

What I did to fix:

$ cd /usr/bin
$ sudo rm tsc
$ sudo npm install -g typescript@latest

Calling 'tsc' worked without errors after this.

发布评论

评论列表(0)

  1. 暂无评论