Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning

Tags:

typescript

I'm trying to get a basic decorator example to work in TypeScript without any luck.

I'm constantly seeing the error message:

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.

My tsconfig.json looks as follows (tsconfig in the same folder as index.ts):

{
    "compilerOptions": {
        "target": "esnext",
        "watch": true,
        "experimentalDecorators": true,
    }
}

This seems to be a pretty popular problem and has been asked multiple times on SO e. g.: Experimental decorators warning in TypeScript compilation

I have tried all the solutions without luck. Also, my problem does not seem to be related to vscode. I get the same error trying to run the file from the shell.

What I've tried so far:

  1. Restart VC Code
  2. EmitDecoratorMetaData: true
  3. Allow experimental decorators as an implicit config in VC Code in VC code
  4. Set TS version manually
  5. Create new tsconfig.json

EDIT

index.ts

function f(): any {
    console.log("f(): evaluated");
    return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
        console.log("f(): called");
    }
}

function g(): any {
    console.log("g(): evaluated");
    return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
        console.log("g(): called");
    }
}

class C {
    @f()
    @g()
    method() {}
}
like image 201
Xen_mar Avatar asked Dec 02 '22 09:12

Xen_mar


2 Answers

I noticed that VS Code has an option in the settings to enable experimentalDecorators for files not a part of a project. Are your code files a part of your project? VS Code Experimental Decorators Screenshot

like image 82
Kameron Kincade Avatar answered Jun 01 '23 19:06

Kameron Kincade


You can remove the warning from the CLI with the flag explicitly provided to the command line:

tsc index.ts --experimentalDecorators

An alternative is to list the index.ts in the files section of the tsconfig.

{
  "files": ["index.ts"],
  "compilerOptions": {
    "target": "esnext",
    "experimentalDecorators": true
  }
}

You can call the CLI without any arguments and it should run without errors.

tsc 

I didn't manage to reproduce the error in VS Code though.

like image 41
Samuel Vaillant Avatar answered Jun 01 '23 17:06

Samuel Vaillant