10

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!

hans
  • 1,001
  • 1
  • 11
  • 14
  • 1
    This is a known breaking change in 0.9.5 see the [Rest arguments are now properly checked for function arity](https://typescript.codeplex.com/wikipage?title=Known%20breaking%20changes%20between%200.8%20and%200.9) – nemesv Dec 10 '13 at 20:46

1 Answers1

18

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

basarat
  • 261,912
  • 58
  • 460
  • 511
  • It's actually quite a bit cleaner to just pass an object in REST format than to use variable parameters, because with variable parameters you have integer positions instead of names of properties/variables which is easy to mess up, harder to read, and impossible to have an interface that checks if it you want. So you don't need variable parameters since javascript does REST objects natively. –  Jan 28 '17 at 05:16