typescript type parameter T code example

Example 1: typescript parameter function type

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 generic type

function identity<T>(arg: T): T {
  return arg;
}Try

Example 3: how to make a generic variable in javascript

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