javascript - Typescript: why function without parameters can be cast to function with parameters -


why there aren't compilation errors in code? code:

function partial(f: (a: string) => string, a: string) : string {   return ""; }  var test = () => ""; var result = partial(test, ""); 

function "partial" takes first argument function, takes 1 parameter, pass function, doesn't take parameters, , typescript compiler thinks, ok. understand can't break because can pass parameters in world function doesn't take any, , won't break anything, should compilation error because typescript types , there obvious type missmatch , can possible developer's error.

is there workarounds problem?

there obvious type missmatch , can possible developer's error.

there's nothing wrong code. consider this:

let items = [1, 2, 3]; // print each item in array items.foreach(item => console.log(item)); 

is code correct? definitely! foreach invokes provided function three arguments, not one. tedious have write:

items.foreach((item, unused1, unused2) => console.log(item)); 

note can still errors if try that's wrong. example:

function printnumber(x: number) { console.log(x); } let strings = ['hello', 'world']; strings.foreach(printnumber); // error, can't convert string number 

Comments