You can easily extend existing types as follows:
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 as you require.
But it can be done like this:
interface ArrayConstructor { from(arrayLike: any, mapFn?, thisArg?): Array<any>; }
Then you can use Array.from .
Try it on ; } var a: any [] = Array.from ([1,2,3]); rel = noreferrer> playground .
edit
If you need to fill out an implementation (since the environment in which you are going to work does not have one), then here's how:
interface ArrayConstructor { from(arrayLike: any, mapFn?, thisArg?): Array<any>; } Array.from = function(arrayLike: any, mapFn?, thisArg?): Array<any> {
Polyfill code in MDN .
2nd editing
Based on the comment, I add 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>; }
This is an exact copy of how it is defined in lib.es6.d.ts.
Nitzan tomer
source share