33

I was googling, but cannot find the information what and how should I add to my project to allow me using ES6 methods like Array.from

__ EDIT: removed prototype word

2
  • 1
    'Array.from' is supported in most browsers, but you can find a polyfill in MDN Commented Apr 19, 2016 at 9:33
  • 2
    The question is how to add it to TypeScript definitions... I don't need polyfill, I use Babel and TypeScript to translate my ES6 code to ES5. Just TypeScript has no definition, for newest methods... Commented Apr 19, 2016 at 13:15

4 Answers 4

23

If you are sure the API exists on your engine at runtime, compile with --lib es6 (or --lib dom,es6 if you are using the DOM APIs).

See Compiler Options documentation for more details.

Sign up to request clarification or add additional context in comments.

1 Comment

Please remove the space to make it like this '--lib dom,es6'.
12

You can easily extend existing types like so:

interface Array {
    from(arrayLike: any, mapFn?, thisArg?): Array<any>;
}

The problem here is that this will add the function to the array instances and not as a static function like you require.
But that can be done like so:

interface ArrayConstructor {
    from(arrayLike: any, mapFn?, thisArg?): Array<any>;
}

Then you should be able to use Array.from.

Try it out on Playground.


Edit

If you need to polyfill the implementation (because the environment in which you intend to run doesn't have it), then this is how:

interface ArrayConstructor {
    from(arrayLike: any, mapFn?, thisArg?): Array<any>;
}

Array.from = function(arrayLike: any, mapFn?, thisArg?): Array<any> {
    // place code from MDN here
}

The polyfill code in MDN.


2nd edit

Based on a comment, I'm adding a typed version:

interface ArrayConstructor {
    from<T, U>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): Array<U>;
    from<T>(arrayLike: ArrayLike<T>): Array<T>;
}

It's an exact copy of how it's defined in the lib.es6.d.ts.

19 Comments

Does it mean that there is no "standard" way of adding ES6 methods definitions to TS? I tried to find it here: definitelytyped.org/tsd but without any luck... Is this next "great" thing, made by MS?
@Kania What's not standard about it? The language gives you the ability to add methods when needed, what else can you ask for?
ES6 is not a private project. Why should I add defined by ES6 methods manually? maybe I expect too much from MS.
There are quite a few things that people would like to be added to typescript, and things are done over time, but you can't expect it to have it all.. But since you can very easily add it yourself, maybe it's not on their top priorities
@SandyGifford My bad, I forgot to copy the other signature for it (which doesn't include the mapping). I fixed my answer.
|
1

I hope it isnt terribly off topic but I found this while refactoring js to ts, and if you have an array like, it appears to work simply passing the array like to the constructor instead of using from, removing the need for the extra method.

eg

// someScript.js

Array.from( arrayLikeThing );

becomes

// someScript.ts

Array( arrayLikeThing );

If there are other reasons to maintain using .from then the above answers are great.

3 Comments

As a warning, just found out this doesnt compile to array.from, so may be best to jsut reimplement like above
Array.from(Array(3)).map(v => 1) // [1, 1, 1]
Array(Array(3)).map(v => 1) // [1]
0

added below line to tsconfig.json inside compilerOptions solved for me.

"lib": [ "DOM", "ES2015.Core", "DOM.Iterable", "ES2015.Collection", "ES2015.Iterable", "ES2015" ]

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.