Course contentsShow
← TypeScript
Lesson 78 of 1,7962. Setup and ToolingFree lesson

The --target Flag: Choosing Your JavaScript Version

Learn how --target controls which JavaScript features TypeScript compiles down to for browser compatibility.

The --target Flag: Choosing Your JavaScript Version

What you'll learn: How to control which version of JavaScript TypeScript compiles your code into for browser and runtime compatibility.

What Does --target Do?

When TypeScript compiles your code to JavaScript, it needs to know which JavaScript version to produce. The --target flag (or target in tsconfig.json) tells TypeScript whether to output modern ES2022 code or older ES5 code that works in ancient browsers.

Think of it like choosing a language dialect: you might speak differently to someone using old technology versus someone with the latest tools.

Why This Matters

Browsers and Node.js versions support different JavaScript features. Modern environments understand async/await, arrow functions, and classes. Older browsers (like Internet Explorer) don't. TypeScript can transform your modern code into older syntax so it runs everywhere.

Common --target Values

  • ES5: Maximum compatibility (IE11, very old browsers)
  • ES6/ES2015: Classes, arrow functions, promises
  • ES2017: Async/await support
  • ES2020+: Modern features like optional chaining
  • ESNext: Latest proposed features

You set this in tsconfig.json like you learned earlier:

{
  "compilerOptions": {
    "target": "ES2020"
  }
}

Or via command line: tsc --target ES2020

The Trade-off

Lower targets produce more compatible but verbose code. Higher targets produce cleaner code but require modern runtimes. Choose based on your deployment environment.

Key Takeaway: The --target flag controls which JavaScript version TypeScript compiles to, balancing modern syntax with browser/runtime compatibility.