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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -