Does the TypeScript compiler also handle ES6 syntax, or do I need Babel for that?
I have a different perspective on Fylax's response. The TypeScript compiler is capable of converting ES6 syntax to ES3 or ES5 without requiring an additional tool like Babel.
TypeScript transpiles new features such as `let`, `for ... of` loops, arrow functions, rest parameters, etc., into ES3 or ES5. However, it does not include polyfills by default. To use modern APIs like `Promise` on older ES3 or ES5 virtual machines, you need to:
- Integrate a polyfill (e.g., [es6-promise](https://github.com/stefanpenner/es6-promise)) to make the API accessible;
- Instruct the compiler to utilize standard typings for this API.
This approach offers flexibility and control. With TypeScript, you can carefully select necessary polyfills and test them across various target browsers.
By default, when targeting ES3 or ES5, the compiler does not incorporate definitions for recent ECMAScript APIs. Refer to [the documentation](http://www.typescriptlang.org/docs/handbook/compiler-options.html) for more details:
Note: If `--lib` is unspecified, a default library is injected. The default libraries are:
► For `--target ES5`: `dom, es5, scripthost`
If a polyfill exposes an API, we can configure the compiler to leverage it. Below is an example `tsconfig.json` configuration for using promises in an ES5 VM:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "es5", "es2015.promise"]
}
}
Nevertheless, Babel has slightly broader support for converting features to ES5 compared to TypeScript. Refer to [Kangax's compatibility table](http://kangax.github.io/compat-table/es6/) for further insights.