0

One of the main benefits of Typescript listed on typescriptlang.org's homepage is the ability to write "State of the Art Javascript" that will compile to any ES3 or newer target. I can use many ES6 functions in Typescript, including let and const, arrow functions, destructuring, template literals, etc., etc.

I'm trying to understand why certain ES6 features are not available in Typescript. Is there a roadmap specifying when certain ES6 features will be added to Typescript, or an explanation regarding why they might not be?

For example, trying to use String.includes in Typescript causes an error, and the resulting javascript is not transpiled and fails in browsers that don't support the method.

The transpiled version (with target set to ES5 in tsconfig.json) of this fails in Opera:

let homer = "homer simpson";

if(homer.includes("homer")) {
    alert("Yes! We have a homer!");
}

I can add my own polyfill and it works:

// Polyfill for .includes 
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }

    if (start + search.length > this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}

let homer = "homer simpson";

if(homer.includes("homer")) {
    alert("Yes! We have a homer!");
}

A few examples of ES6 features that are implemented in Typescript

Template Literal

let myDiv = `
    <div>
        <h2>I am part of a template literal.</h2>
    </div>
`

transpiles to

var myDiv = "\n    <div>\n        <h2>I am part of a template literal.</h2>\n    </div>\n";

Destructuring

let [a,b] = [1,2,3];

transpiles to

var _a = [1, 2, 3], a = _a[0], b = _a[1];

Arrow Functions

const arrsq = [1,2,3];
const squares = arrsq.map(x => x*x);

transpiles to

var arrsq = [1, 2, 3];
var squares = arrsq.map(function (x) { return x * x; });
Jon Crowell
  • 21,695
  • 14
  • 89
  • 110
  • 2
    TypeScript is transpilable to a number of environments. Without context info, it has no way of knowing if given features are already present in that environment; and since NodeJS is a common use factor, it doesn't make sense to always include all polyfills. (Note these are things you can set in tsconfig.json). The best solution to this is to include a standard set of ES6 polyfills with any web projects you intend to make. – Katana314 Jun 14 '16 at 20:03
  • TypeScript is a super-set of javasript. So anything javascript that does/doesn't work in a given browser will work/notwork in typescript. –  Jun 14 '16 at 20:05
  • Thanks, @Katana314, that is exactly what I was looking for. – Jon Crowell Jun 14 '16 at 20:12
  • That may be, @Austin, but it is confusing. For example, using `let` in Typescript transpiles to `var` and works fine in older browsers. – Jon Crowell Jun 14 '16 at 20:15
  • The answer is 'never', I guess. Polluting transpiled code with polyfills isn't Typescript's job. It's job is to transpile the features that are allowed for current traget. – Estus Flask Jun 14 '16 at 20:24
  • `let` is part of the `ES6` so typescript help you use it even if you're targeting `ES5` by substituting it with `var` in the compiled js, but if you'll target `ES6` then it will keep the `let`. The same with fat arrows. – Nitzan Tomer Jun 14 '16 at 20:25
  • @JonCrowell Good point. TypeScript usually transpiles any kinds of "syntax features", like the arrow syntax, as long as they can be expressed in JavaScript with perhaps renamed variables, etc. Note that if you target ES6, it won't do anything to translate these features. It won't go so far as to implement missing types, libraries, or functions though. – Katana314 Jun 14 '16 at 20:26
  • @basarat, can you shed any light on this? – Jon Crowell Jun 14 '16 at 21:27

1 Answers1

0

When will ES6 methods like String.includes or Number.isInteger be included in Typescript

They already are. You can use typescript@next with --lib es6

basarat
  • 261,912
  • 58
  • 460
  • 511