编辑代码

console.log("泛型 -- ts")

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

let output = identity<number>(1);  // type of output will be 'string'

console.log(output)

function add<U>(arg: Array<U>): Array<U> {
    console.log(arg.length)
    return arg
}
let _key = Symbol('a')

console.log(add([]))

function add_1<T>(arg: T): T {
    return arg
}
let myAdd_1: <T>(arg: T) => T = add_1
console.log(myAdd_1('aa'))
let myAdd_2: {<T>(arg: T): T} = add_1
console.log(myAdd_2('aa-22'))

interface Gener {
    <T>(arg: T): T
}

let myAdd_3: Gener = add_1
console.log(myAdd_3('add-3'))

interface Gener_1<T> {
    (arg: T): T
}
let myAdd_4: Gener_1<Date> = add_1
console.log(myAdd_4(new Date))

// 泛型类
class GenericNumber<T> {
    zeroValue: T;
    add: (x: T, y: T) => T
}
// let class_1 = new GenericNumber<number>()
// class_1.value = 0
// class_1.add = (x, y) => x + y
// console.log(class_1.add(1,2))

let stringNumeric = new GenericNumber<string>();
stringNumeric.zeroValue = "";
stringNumeric.add = function(x, y) { return x + y; };

console.log(stringNumeric.add(stringNumeric.zeroValue, "test"));