type as parameter 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: how to make a generic variable in javascript

var x = 1;
var y = 1;
if (x == y) {
  x = x + 1
}

Tags:

Misc Example