hans
hans

Reputation: 1031

typescript variable number of parameters in function interface definition 0.9.5

I have this scenario, the file is 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; };

When I compile this with tsc 0.9.5 I get the following errors:

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.

Am I missing something or is this broken? It used to work in 0.9.1.1. thanks!

Upvotes: 10

Views: 12845

Answers (1)

basarat
basarat

Reputation: 276189

Since rest parameters are optional, you need to make them optional in your functions as well:

   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; };

This is new in TS0.9.5

Upvotes: 18

Related Questions