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!
Function Type Expressions The syntax (a: string) => void means “a function with one parameter, named a , of type string, that doesn't have a return value”. Just like with function declarations, if a parameter type isn't specified, it's implicitly any . Note that the parameter name is required.
TypeScript Interface can be used to define a function type by ensuring a function signature. We use the optional property using a question mark before the property name colon. This optional property indicates that objects belonging to the Interface may or may not have to define these properties.
An interface type cannot be passed as a parameter. When running TypeScript code, you are really compiling it down to JavaScript and then running the JavaScript. An interface is a TypeScript compile-time construct, so at runtime, there is no such thing as an interface type to call functions on or inspect properties of.
In Typescript, making optional parameters is done by appending the “?” at the end of the parameter name in the function when declaring the parameters and the parameters which are not marked with “?” i.e not optional parameter are called as default parameters or normal parameters where it is must and compulsory to pass ...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With