我有这个场景,文件是t.ts:
interface Itest{
(event: any, ...args: any[]):any;
}
var t1: Itest = function (testparam) { return true;};
var t2: Itest = function (testparam, para1) { return true; };
var t3: Itest = function (testparam, para1, para2) { return true; };
interface Itest2 {
(event: any, para1,...args: any[]): any;
}
var t4: Itest2 = function (testparam, para1) { return true; };
var t5: Itest2 = function (testparam, para1, para2) { return true; };
当我用tsc 0.9.5编译它时,我得到以下错误:
tsc --target ES5 "t.ts"
t.ts(6,8): error TS2012: Cannot convert '(testparam: any, para1: any) => boolean' to 'Itest':
Call signatures of types '(testparam: any, para1: any) => boolean' and 'Itest' are incompatible:
Call signature expects 1 or fewer parameters.
t.ts(7,8): error TS2012: Cannot convert '(testparam: any, para1: any, para2: any) => boolean' to 'Itest':
Call signatures of types '(testparam: any, para1: any, para2: any) => boolean' and 'Itest' are incompatible:
Call signature expects 1 or fewer parameters.
t.ts(14,8): error TS2012: Cannot convert '(testparam: any, para1: any, para2: any) => boolean' to 'Itest2':
Call signatures of types '(testparam: any, para1: any, para2: any) => boolean' and 'Itest2' are incompatible:
Call signature expects 2 or fewer parameters.
我错过了什么或者这个坏了吗?它曾用于0.9.1.1。谢谢!
答案 0 :(得分:15)
由于rest参数是可选的,你需要在函数中使它们可选:
interface Itest{
(event: any, ...args: any[]):any;
}
var t1: Itest = function (testparam?) { return true;};
var t2: Itest = function (testparam?, para1?) { return true; };
var t3: Itest = function (testparam?, para1?, para2?) { return true; };
interface Itest2 {
(event: any, para1,...args: any[]): any;
}
var t4: Itest2 = function (testparam, para1) { return true; };
var t5: Itest2 = function (testparam, para1, para2?) { return true; };
这是TS0.9.5中的新内容