10

Array.from is an ES6 feature. When I use it in TypeScript and compile to ES5 target it does not change it:

tsc -t es5 prog.ts

i.e. when I look inside prog.js I still see Array.from in the same place. Using prog.js in IE11 complains as follows:

Object doesn't support property or method 'from'

Why doesn't TypeScript convert Array.from in to some ES5 alternative?

Is there a way to set it up so it does?

2
  • It is a method that can be polyfilled. Language features that can't be polyfilled are transpiled. Commented Apr 14, 2016 at 12:17
  • I see. I did not know that. I expected transpiling to take care of converting all code to some ES5 equivilant. But, yes, it can easily be polyfilled. Thanks. Post an answer and I will accept Commented Apr 14, 2016 at 12:20

3 Answers 3

12

I would recommend using core-js as you'll get many more polyfills and won't have to polyfill APIs piecemeal. If all you need/want is Array.from, then by all means rip it from the MDN site, but you can selectively import just the polyfills you need using core-js.

Using npm to install core-js...

> npm install --save core-js

...then in your .ts...

import 'core-js' // to get the whole thing

or

import 'core-js/es/array'; // to get just array polyfills for example
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Your tip helped me a lot. BTW: actually, it's import 'core-js/es6/array';
it's import 'core-js/es/array'; now
8

It is a method that can be polyfilled.

Language features that can't be polyfilled are transpiled (if possible on the selected TypeScript target).

Comments

4

The Array.from doesn't yet exist in TypeScript v1.8 so the compilator leaves as-is this part of code.

According to this link Array.from isn't supported on IE, so you have to implement a polyfill (see the link, the polyfill is in).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.