What You'll Learn
You'll learn how to control which version of JavaScript TypeScript compiles your code into using the target compiler option.
Why This Matters
TypeScript code doesn't run directly in browsers or Node.js—it gets compiled into plain JavaScript first. But which flavor of JavaScript? That's where target comes in.
Think of it like a translator who can speak to people from different time periods. Modern JavaScript (ES2020+) has sleek features like optional chaining (?.), but older browsers don't understand it. Setting target tells TypeScript: "translate my code so this version of JavaScript can understand it."
The Core Concept
In your tsconfig.json, the target option lives inside compilerOptions:
{
"compilerOptions": {
"target": "ES2020"
}
}
Common target values:
- ES5: Maximum compatibility (IE11, very old browsers)
- ES2015 (or ES6): Modern baseline (arrow functions, classes)
- ES2020: Recent features (optional chaining, nullish coalescing)
- ESNext: Absolute latest JavaScript proposals
Choosing Your Target
Ask yourself: "Where will this JavaScript run?"
- Building for old browsers? Use
ES5 - Modern web apps with build tools?
ES2015orES2020 - Latest Node.js versions?
ES2020orESNext
Higher targets produce cleaner, smaller code but require modern environments. Lower targets work everywhere but include more "polyfill" code that mimics newer features.
Example Impact
Your TypeScript const greet = () => "Hello" becomes:
- ES2015:
const greet = () => "Hello";(unchanged) - ES5:
var greet = function() { return "Hello"; };(transformed)
Key Takeaway: The target option determines which JavaScript version TypeScript compiles to—choose based on where your code will actually run.