Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decorators error in VS code editor after upgrading to Angular 10

Error: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.

In Angular 9 with same project no such error was there.

Angular10 has now added tsconfig.base.json file.

tsconfig.json file updated as:

{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ]
}

tsconfig.app.json:

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

tsconfig.base.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
  }
}

I noticed that content of tsconfig.json in Angular 9 has been moved to tsconfig.base.json in Angular 10.

like image 443
Abhishek Ranjan Avatar asked Jun 27 '20 19:06

Abhishek Ranjan


People also ask

How do I remove Experimentaldecorator warning in VSCode?

In VSCode, Go to File => Preferences => Settings (or Control+comma) and it will open the User Settings file. Search "javascript. implicitProjectConfig. experimentalDecorators": true and then check the checkbox for experimentalDecorators to the file and it should fix it.

What is experimental decorators in TypeScript?

Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members. Decorators are a stage 2 proposal for JavaScript and are available as an experimental feature of TypeScript. NOTE Decorators are an experimental feature that may change in future releases.

What are decorators in AngularJS?

Decorators are design patterns used to isolate the modification or decoration of a class without modifying the source code. In AngularJS, decorators are functions that allow a service, directive, or filter to be modified before it is used. Each decorator has a unique role.

How to enable/disable experimental decorators in VSCode?

In VSCode go to preferences -> settings, you will see an option to enable/disable experimentalDecorators. Check it and save the settings file. Done Create tsconfig.json file in the root directory of your project and include the following options.

How do I use injectable Decorators with syncservice in angular?

In an Angular (v10) project creating a blank file (sync.service.ts) and adding this code: import { Injectable } from '@angular/core'; @Injectable ( { providedIn: 'root' }) export class SyncService { } Experimental support for decorators is a feature that is subject to change in a future release.

How do I turn off experimental decorators support?

Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning. I opened a random file (tag-cloud.component.ts) and added an import statement: The warning goes away.


3 Answers

I was having the same issue in VSCode with Angular 10 freshly installed, I checked and experimental decorator was already added, but still, I was getting that annoying warning and the squiggly line, so what I did, I simply saw that vscode is using an old version of typescript so, I click on bottom-right of vscode where it was showing version 3.8.3, and I changed it to the latest version, which was installed with angular, and my problem gets solved.

let me know if it helps and resolved your issue as well.

Thank You

like image 164
imran qasim Avatar answered Oct 20 '22 15:10

imran qasim


It generally happens if you open the vs code from wrong directory. (Eg opening VScode from app folder instead of root).

If directory issue isn't the case, Please restart the Vs code.

If still you are seeing the same issue, then do the following :

  • Go to File => Preferences => Settings (or Control+comma) and it will open the User Settings file. Search "javascript.implicitProjectConfig.experimentalDecorators" and then check the checkbox for experimentalDecorators

or,

  • Add the following in VScode settings.json

"javascript.implicitProjectConfig.experimentalDecorators": true

like image 34
Akhil Avatar answered Oct 20 '22 14:10

Akhil


TLDR

Import your decorated class into some other file that is already included in TypeScript compilation context which starts from src/main.ts file.

Explanation

Angular 10 uses TypeScript 3.9 which uses "Solution style" for tsconfig.json files. This tsconfig.json file is used by editors and TypeScript’s language server. Now this file can include several references to other tsconfig files, e.g. tsconfig.app.json and tsconfig.spec.json.

Inside those configs we have files and include options defined like:

"files": [
  "src/main.ts",
  "src/polyfills.ts"
],
"include": [
  "src/**/*.d.ts"
]

These two options define TypeScript compilation context. Now TypeScript will compile those files and any files that are referenced from already include in compilation files. And your editor also follows that rule.

If you created some file and define some class with decorator but didn't import it in any included in compilation file then you will get error.

Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.

The reason for this is that for that specific file TypeScript doesn't see experimentalDecorators option.

Solution here is: you should import your class in some other class that is included to compilation context.

Prior to Angular 10 we had the following tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    ...
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
  }
}

We don't see any files or include sections here which means that

If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'.

And that's the reason you didn't see that error in Angular 9. A newly created file was immediately included in compilation context.

like image 17
yurzui Avatar answered Oct 20 '22 13:10

yurzui