You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

17 lines
433 B

// 使用泛型创建一个函数
function id<Type>(value: Type) : Type { // <Type> 相当于是一个占位符
return value
}
// 调用泛型函数:
// 1.以 number 类型调用泛型函数
const num = id<number>(10)
// 2.以 string 类型调用泛型函数
const str = id<string>('a')
const ret = id<boolean>(false)
// 泛型简写
let num1 = id(100) // number类型
let str1 = id('abc') // string类型