Typescript lib.d.ts Arrray.prototype.find

Typescript lib.d.ts Arrray.prototype.find,typescript,ecmascript-6,Typescript,Ecmascript 6,我们正在开发一款预计至少一年内不会投产的web应用程序,因此我们正在尽可能地进行前瞻性思考 我们用它来访问或。问题是如何让我们的打字脚本代码发挥作用 例如,ts 1.4中的lib.d.ts不知道Array.prototype.find。我抓取了Typescript源代码,在bin文件夹中有一堆d.tlib.core.es6.d.ts和lib.es6.d.ts都有find。我试着在我们的构建中使用它们,typescript编译器会在它们上加上barfs(“接口中不允许使用计算属性名”) 在Type

我们正在开发一款预计至少一年内不会投产的web应用程序,因此我们正在尽可能地进行前瞻性思考

我们用它来访问或。问题是如何让我们的打字脚本代码发挥作用

例如,ts 1.4中的lib.d.ts不知道
Array.prototype.find
。我抓取了Typescript源代码,在
bin
文件夹中有一堆d.t
lib.core.es6.d.ts
lib.es6.d.ts
都有
find
。我试着在我们的构建中使用它们,typescript编译器会在它们上加上barfs(“接口中不允许使用计算属性名”)

在Typescript中获得ES6类型支持的最佳方法是什么?

诀窍是只复制您需要的内容。在您的情况下,只需使用
find
方法

interface Array<T> {
        /** 
      * Returns the value of the first element in the array where predicate is true, and undefined 
      * otherwise.
      * @param predicate find calls predicate once for each element of the array, in ascending 
      * order, until it finds one where predicate returns true. If such an element is found, find 
      * immediately returns that element value. Otherwise, find returns undefined.
      * @param thisArg If provided, it will be used as the this value for each invocation of 
      * predicate. If it is not provided, undefined is used instead.
      */
    find(predicate: (value: T, index: number, obj: Array<T>) => boolean, thisArg?: any): T;

    /** 
      * Returns the index of the first element in the array where predicate is true, and undefined 
      * otherwise.
      * @param predicate find calls predicate once for each element of the array, in ascending 
      * order, until it finds one where predicate returns true. If such an element is found, find 
      * immediately returns that element value. Otherwise, find returns undefined.
      * @param thisArg If provided, it will be used as the this value for each invocation of 
      * predicate. If it is not provided, undefined is used instead.
      */
    findIndex(predicate: (value: T) => boolean, thisArg?: any): number;
}

var foo = [];
var bar = foo.find((x)=>true);
接口数组{
/** 
*返回数组中谓词为true且未定义的第一个元素的值
*否则。
*@param predicate find按升序为数组的每个元素调用一次谓词
*顺序,直到找到谓词返回true的元素。如果找到这样的元素,则查找
*立即返回该元素值。否则,find返回undefined。
*@param thisArg如果提供,它将用作每次调用的this值
*谓词。如果未提供,则使用undefined。
*/
find(谓词:(value:T,index:number,obj:Array)=>boolean,thisArg?:any):T;
/** 
*返回数组中谓词为true且未定义的第一个元素的索引
*否则。
*@param predicate find按升序为数组的每个元素调用一次谓词
*顺序,直到找到谓词返回true的元素。如果找到这样的元素,则查找
*立即返回该元素值。否则,find返回undefined。
*@param thisArg如果提供,它将用作每次调用的this值
*谓词。如果未提供,则使用undefined。
*/
findIndex(谓词:(值:T)=>布尔,thisArg?:any):数字;
}
var foo=[];
var bar=foo.find((x)=>true);

这是我自己得出的结论。我想更广泛的问题是我是不是找错人了?我想其他人也在一条类似的船上。是否有人为ES6/Typescript构建了一个polyfill包来为我实现这一点,而我必须维护自己的自定义.d.ts文件?不仅仅是
数组。查找
。例如,shim支持包含迭代器的
Map
,但
lib.es6.d.ts
定义包含依赖于计算属性的迭代器定义。