0

I hope I am not missing a point here. But how can I keep ES6 syntax when transpiling to JS. For instance, if I code:

class Person {
  public name: string;
  constructor(name: string) {
    this.name = name;
  }
}
let person = new Person('John Doe');
console.log(person.name);

TS gives me: ("target": "es6" in tsconfig.json)

var Person = /** @class */ (function () {
    function Person(name) {
        this.name = name;
    }
    return Person;
}());
var person = new Person('John Doe');
console.log(person.name);

But I want TS to give me:

class Person {
  constructor(name) {
    this.name = name;
  } 
}
let person = new Person('John Doe');
console.log(person.name);

My tsconfig.json is like this:

{
  "compilerOptions": {
    "target": "es6",
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true
  }
}

P.S.: If I run the command:

tsc -t es6 app.ts

It works;

8
  • How are you doing your transpiling? Commented Jun 28, 2019 at 14:32
  • 1
    "target": "es6" should work afaik but you can also try "target": "es2015" which is the official name of ES6. In any case it seems like your target is not properly set or being picked up by the compiler. Commented Jun 28, 2019 at 14:35
  • @PietroNadalini I have a tsconfig.json with "{"compilerOptions":{"target":"es6"}}". Commented Jun 28, 2019 at 14:38
  • 1
    The compiler is obviously just not loading the config file, or the file is broken. That's the problem you need to be solving. Commented Jun 28, 2019 at 14:55
  • 1
    @Paleo I've edit the content and add the configs. Commented Jun 28, 2019 at 15:13

1 Answer 1

2

Your file tsconfig.json is ignored when you specify app.ts in the command line.

In tsconfig.json, add a section exclude after the compiler options:

{
  "compilerOptions": {
    ...
  },
  "exclude": [
    "node_modules"
  ]
}

Then, just run: tsc.

See also the documentation:

Using tsconfig.json

  • By invoking tsc with no input files […]
  • By invoking tsc with no input files and a --project (or just -p) command line option […]
Sign up to request clarification or add additional context in comments.

1 Comment

Yep! That solved and he output is ES6 as desired. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.