donnut
donnut

Reputation: 720

TypeScript - When to use target version?

I am having a difficulty understanding what the meaning of the tsc target version is (ES3 versus ES5).

In TypeScript output still uses Array.prototype.reduce even though I target ES3 it says one should read it as a language spec, but doesn't clear things up a lot. As far as I have tried, setting the --target doesn't have any effect of the output nor the warning/error messages.

Am I correct to think that this option is to support a Visual Studio feature?

Upvotes: 2

Views: 746

Answers (2)

Niko
Niko

Reputation: 26730

TypeScript is a superset of JavaScript and the compiler therefore only touches non-JavaScript bits, which need to be replaced by JavaScript code. The "target" flag is only used to tell the compiler which features it may use here. For example, TypeScript classes with property accessors will not compile if you target ES3 as the compiler is not able to transform the TypeScript bits of

class Foo {
    public get bar(): string {
        return 'Bar';
    }
}

into valid ES3 JavaScript.

Upvotes: 3

Paleo
Paleo

Reputation: 23702

Array.prototype.reduce is an API. The TypeScript compiler provides no API. A TypeScript developer needs to know JavaScript and its API. In the same way as CoffeeScript: "It's just JavaScript".

For example, in a TS code, the API Array.prototype.reduce can be used, then the code converted to the ES3 syntax. It will work on IE8 with es5-shim.

Upvotes: 1

Related Questions