1function firstElement<Type>(arr: Type[]): Type {
2 return arr[0];
3}
4// s is of type 'string'
5const s = firstElement(["a", "b", "c"]);
6// n is of type 'number'
7const n = firstElement([1, 2, 3]);
1class GenericNumber<T> {
2 zeroValue: T;
3 add: (x: T, y: T) => T;
4}
5
6let myGenericNumber = new GenericNumber<number>();
7myGenericNumber.zeroValue = 0;
8myGenericNumber.add = function(x, y) { return x + y; };
9