4
votes

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() {}
}
3
Could you share which version of TypeScript is used? Could you also share the module that you want to compile? It would help to reproduce the error.Samuel Vaillant
TS Version 3.8.3Xen_mar
I added the moduleXen_mar
You have the issue in VS Code too or only with the CLI?Samuel Vaillant
Both produce the same error.Xen_mar

3 Answers

10
votes

I noticed that VS Code has an option in the setting 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

1
votes

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.

0
votes

I was facing this error in watch mode. Manually updating the tsconfig.json file with experimentalDecorators:true did not fix it for me. Solution that worked is

kill the watch terminal. or Press ctrl C run: tsc app.ts --experimentalDecorators --watch

app.js is your TS entry file. I checked my tsconfig.json file after that and found experimentalDecorators is set to true.