Why below interface code of TypeScript executes? -
in below code snippet, have declared imath interface , implemented in add method , works, absolutely fine.
however, in multiply method should have given compile error passing 1 parameter , 2nd parameter not optional.
interface imath { (a: number, b: number): number; } // using interface var add: imath; add = function (a: number, b: number) { return + b; } var sum = add(5, 3); alert(sum); var multiply: imath; multiply = function (c: number) { return c; } var result = multiply(5, 3); alert(result);
but not compile error in visual studio, guess?
this because function of type (number) -> number
can assigned type of (number, number) -> number
. it’s second argument ignored , thrown away doesn’t mean not call 2 arguments, making assignment valid.
see following minimal example shows valid assignment:
var f: (a: number, b:number) => number = function (a:number):number { return 0; } f(1, 2);
Comments
Post a Comment