我们正在制作一个不希望看到生产至少一年的网络应用程序,所以我们尽量做到前瞻性思维。
我们正在使用此ES6 polyfill来访问Map
或Array.prototype.find
等内容。问题是让我们的Typescript代码一起发挥。
例如,TS 1.4中的lib.d.ts不了解Array.prototype.find
。我抓住了Typescript源代码,bin
文件夹中有一堆d.ts。 lib.core.es6.d.ts
和lib.es6.d.ts
都有find
。我尝试在我们的构建中使用它们,并在它们上使用typescript编译器barfs(“接口中不允许使用计算属性名称”)。
在Typescript中获得ES6类型支持的最佳方法是什么?
答案 0 :(得分:3)
诀窍是仅复制你需要的东西。在您的情况下只是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);