call function typescript code example

Example 1: typescript pass a function as an argunetn

class Foo {
    save(callback: (n: number) => any) : void {
        callback(42);
    }
}
var foo = new Foo();

var strCallback = (result: string) : void => {
    alert(result);
}
var numCallback = (result: number) : void => {
    alert(result.toString());
}

foo.save(strCallback); // not OK
foo.save(numCallback); // OK

Example 2: typescript function type

interface Date {
  toString(): string;
  setTime(time: number): number;
  // ...
}

Example 3: html call typescript function

myFunction(param1: string) {
    console.log('Everything is ok...');
    return param1 + 1;
}

<button (click)='myFunction("parameter")'>Button Click</button>