1

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';
4
  • 1
    What's your tsconfig? Also, no, because all types are removed at compile time. Commented Jun 23, 2021 at 19:14
  • That second example isn't JavaScript and it's identical to the original code. Commented Jun 23, 2021 at 19:15
  • 4
    type annotations (: number, : string) are features of TypeScript, not ES6 Commented Jun 23, 2021 at 19:15
  • yeah, sorry, was a copy/paste error by my part, fixed. Commented Jun 24, 2021 at 3:02

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

In my case, after adding tsconfig.js and set

{
  "compilerOptions": {
    "target": "es2015"
  }
}

still not working.

Then I run npx tsc instead of npx tsc xxx.ts, then I get 'const'

Comments

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.