2

I was playing around with TypeScript a bit and found a little oddity. Let's say I am building a little application that (sadly) needs to support IE8, too. So I set the TypeScript-compiler to target ES3.

I assumed I could safely use ECMAScript 5.1 goodies such as Array.prototype.reduce in my .ts file and that TypeScript will take care of including some kind of polyfill for that. This does not seem to be the case, though:

function joinStuff (stuff : string[], joiner : string = ' ') {
    return stuff.reduce((previous, current) => 
        previous ? previous + joiner + current : current
    ) || '';
}

will get compiled into:

function joinStuff(stuff, joiner) {
    if (joiner === void 0) { joiner = ' '; }
    return stuff.reduce(function (previous, current) {
        return previous ? previous + joiner + current : current;
    }) || '';
}

Now this is not ECMAScript 3 compatible code! (and it won't run in IE8, of course). What am I missing?

defaude
  • 310
  • 1
  • 11

1 Answers1

2

TypeScript doesn't shim or polyfill any APIs for you. There are lots of readily available polyfill libraries for this purpose.

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
  • I know there's a good polyfill just around every corner. It's just sad that TS silently pumps out non-ES3 code even though you ask it to do so ;) – defaude Oct 16 '14 at 15:37
  • Think of it as the ES3 *language* target if that helps :) – Ryan Cavanaugh Oct 16 '14 at 15:39
  • Here is an example using a slightly modified version of the MDN polyfill (there was one small TypeScript compiler error): http://bit.ly/1xVzYOc – Fenton Oct 16 '14 at 15:53
  • BTW: Babel also doesn't transpile or shim APIs (e.g. 'Promise'). A bit of a shame when you just want stuff to work without maintaining extra dependencies. – He Nrik Jul 12 '15 at 20:13