angulartypescriptangular-cliangular14angular15

Typescript target warnings after Angular 15 update


I updated my angular app to Angular 15. It builds ok - unless some warnings like:

TypeScript compiler options "target" and "useDefineForClassFields" are set to "ES2022" and "false" respectively by the Angular CLI.

My tsconfig.json sets the target to ES6:

{
  ...
  "compilerOptions": {
      "target": "ES6",
      ...
  }
}

The documentation says:

Internally the Angular CLI now always set the TypeScript target to ES2022 and useDefineForClassFields to false unless the target is set to ES2022 or later in the TypeScript configuration.

https://github.com/angular/angular-cli/blob/main/CHANGELOG.md

And my .browserslistrc looks the same for month with no changes since the beginning:

last 1 Chrome version
last 1 Firefox version
last 2 Edge major versions
last 2 Safari major versions
last 2 iOS major versions
Firefox ESR

Thus, how can I get rid of this warning?


Solution

  • I had the same issue and successfully silenced this warning by adding "target": "ES2022" and "useDefineForClassFields": false to my tsconfig. Whether this was a good idea or not will have to await a comment from someone more knowledgeable than me. I worry that this will fail in the same way that yours now has when 2022 becomes 2023 (or whatever comes next). Surely it would be better if it could be left out completely (as I had) if Angular is going to override it anyway. But I may have an incomplete grasp of the issue.

    In your case, you should be able to do (or at least attempt) the same thing in place of ES6 (which I understand to be the same as ES2015). According to the documentation you cited, this is what Angular is doing anyway, regardless of your request, so if you only get the warning and no errors your code should be fine. If you need to restrict things further to the ES6 level, it seems you need to use your .broswerslistrc file to do this, which may also be fine already.

    I think the problem here is that the warning is not helpful, at least to people like you and me, who are the ones receiving it and not knowing what to do about it. Also the weblink that follows it ("To control ECMA version and features use the Browerslist configuration. For more information, see https://angular.io/guide/build#configuring-browser-compatibility ") doesn't seem particularly helpful in addressing the warning, telling us what we should be doing but not what to do to get rid of the warning.