Why Typesript compiler change a const into a var?

Viewed 176

In consts.ts i have

const s1: string = 'test';

I compile it with tsc consts.ts and then in consts.js it became

var s1 = 'test';

Why?

2 Answers

TypeScript compiles to ECMAScript 3 code unless you specify otherwise [doc link], and ECMAScript didn't introduce const until version 6.

So it seems that you don't have any typescript configuration for your project. You have to run

npx tsc --init

and then you will be able to change the target value for compiler option there

{
  "compilerOptions": {
    "target": "es2017"
  }
}
Related