How to enable experimentalDecorators in Typescript

2.6k Views Asked by At

This question is not a duplicate of those questions which ask how to suppress a similar warning issued by the code editors such as VSCode.

My problem is for the Tsc command line compiler warning:

greet.ts:7:7 - error TS1219: Experimental support for decorators is a feature th at is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.

Here is my code:

function doMore(target) {
    target.doMore = true;
}

@doMore
class Test {
    do() {
        console.log('done');
    }
}  


var t = new Test();
t.do();
console.log(t.doMore);

I created the following tsconfig.json in the root directory:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "allowJs": true
    }
}

But tsc still complains.

1

There are 1 best solutions below

0
On BEST ANSWER

The tsc compiler ignores the tsconfig.js when the input files are specified in the command line:

The `tsc greet.ts1 will simply ignore the tsconfig.json file - thus no compiler options specified in the file will be effective.

The tsconfig.json file should be included with the source file paths and the tsc compiler should be called without specifying the source files in order to include the tsconfig.js file in the compilation.

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "target": "ES5"
    },

    "files": [
        "greet.ts"
    ]
}