您可以像这样轻松扩展现有类型:
interface Array {
from(arrayLike: any, mapFn?, thisArg?): Array<any>;
}
这里的问题是,这会将函数添加到数组实例中,而不是像您需要的那样作为静态函数。
但可以这样做:
interface ArrayConstructor {
from(arrayLike: any, mapFn?, thisArg?): Array<any>;
}
那么你应该可以使用Array.from。
在Playground 上试用。
编辑
如果您需要 polyfill 实现(因为您打算运行的环境没有它),那么方法如下:
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.
第二次编辑
根据评论,我正在添加一个打字版本:
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>;
}
它与lib.es6.d.ts 中的定义完全相同。