泛型
// 泛型函数
function identity<T>(arg: T): T{
console.log(arg)
return arg
}
function log<T>(arg: Array<T>): Array<T>{
console.log(arg)
return arg
}
// 泛型接口
interface identityInterface<T> {
(arg: T): T
}
let identity: identityInterface<string> = function (str: string) {
return str;
};
// 泛型类
class GenericNumber<T> {
zeroValue: T;
add: (x: T, y: T) => T;
}
let myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function(x, y) { return x + y; };