DefinitiveTyped提供了一个下划线声明文件,它定义了一个List
接口,并在代码中大量使用它。
// Common interface between Arrays and jQuery objects
interface List {
[index: number]: any;
length: number;
}
interface UnderscoreStatic {
sortBy(list: List, iterator?: any, context?: any): any;
groupBy(list: List, iterator: any): any;
countBy(list: List, iterator: any): any;
}
我正在尝试使用该countBy
功能:
// <reference path="../DefinitelyTyped/underscore/underscore.d.ts" />
declare var _: UnderscoreStatic;
_.countBy([1,2,3], function(item) {
return item%2;
});
当我编译文件时,它会抛出错误:
> tsc commons.ts
> E:/commons.ts(5,0): Supplied parameters do not match any signature of call target:
Could not apply type 'List' to argument 1, which is of type 'number[]'
我不知道为什么会发生这个错误,因为number[]
适合界面List
。
哪里出了问题,如何解决?