Search code examples
typescriptvisual-studio-codeterminaltsc

How can I set the default target es version for the typescript compiler (tsc)?


I've spent a lot of the last 24hrs learning:

  • how Visual Studio Code works
  • how TypeScript works
  • how the TypeScript Compiler (tsc) works
  • how these three pieces of software all fit together

In less than a day (!) I finally succeeded in writing some basic TypeScript and compiling it to Javascript.

My ecstasy was a little muted when I discovered that the TypeScript Compiler (tsc) had not only compiled the static type labels, but had also transpiled:

  • const

into:

  • var

I say muted, but actually I was mildly horrified.

Then I discovered that the default transpilation target for tsc is ECMAScript 3.

It turns out, I can keep my const declarations constant, by invoking tsc with a --target flag:

tsc my-first-typescript.ts --target es6

However. I really don't want to keep writing --target es6 (or --target es11) every time I invoke tsc.

Is there any way I can set the default transpilation target version for tsc so that it doesn't automatically assume I want javascript like it's 1999?


Solution

  • I don't think you can globally change the default (well, you could modify the tsc file, but...), but you can set the options (including target) for any given project using a tsconfig.json file in the project root; details in the Project Configuration section of the TypeScript Handbook. E.g.:

    {
        "compilerOptions": {
            "target": "ES2015"
        }
    }
    

    You don't necessarily have to roll your own, either, the tsconfig/bases project on GitHub is designed to make it easy to get up and running with configs for various project types.