javascripttypescriptecmascript-6configecmascript-5

compile typescript code to es6 instead of es5


Currently learning typescript and I noticed that the output code is always using var. Is it possible to output const and let in the .js file or typescript needs to always output es5 for some reason? Thanks.

EXAMPLE:

// main.ts
const x: number = 2;
let y: string = 'hello';

// main.js
var x = 2;
var y = 'hello';

Is this output possible, if so, how?

// main.js
const x = 2;
let y = 'hello';

Solution

  • You can add compiler flags to specify a target, e.g. --target es6. See this question: How do I transpile TypeScript to ES6?