当前位置:网站首页>Generics of TS
Generics of TS
2022-07-05 20:57:00 【qq_ forty-six million three hundred and two thousand two hundre】
Basic use of generics
// Not clear name Used when the type of
// The representation defines a generic T, Then pass on the reference name yes T type ,myFun The return value of T type ; When myFun It can only be determined when called T Value
function myFun<T>(name: T): T {
return name
}
myFun<string>('hellow')//T by string type
myFun<number>(15)//T by number type
Generics can specify multiple
function myFun<T, K>(name: T, age: K): T{
return name
}
myFun<string, number>('tt', 18)//T by string type ,K by number type
Generic binding interface
interface Inter{
length: number
}
//T extends Inter For generics T Must be Inter Subclasses of
function myFum<T extends Inter>(value: T): number{
return value.length
}
myFum('abc')//'abc'.length String has length attribute , accord with Inter The interface specification
myFum([1,3,9,5,])//[1,3,9,5,].length Array has length attribute , accord with Inter The interface specification
myFum({
length: 5})//{length: 5}.length The object has length attribute , accord with Inter The interface specification
Generic binding class
class myClass<T>{
name: T
constructor(name: T){
this.name = name
}
}
const my = new myClass<string>('tm')
边栏推荐
- 判断横竖屏的最佳实现
- wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
- Prosci LAG-3 recombinant protein specification
- 渗透创客精神文化转化的创客教育
- 教你自己训练的pytorch模型转caffe(三)
- 中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
- Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
- 学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
- 基于flask写一个接口
- Traps in the explode function in PHP
猜你喜欢
随机推荐
启牛2980有没有用?开户安全吗、
渗透创客精神文化转化的创客教育
重上吹麻滩——段芝堂创始人翟立冬游记
phpstudy小皮的mysql点击启动后迅速闪退,已解决
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
leetcode:1755. 最接近目标值的子序列和
How to make ERP inventory accounts of chemical enterprises more accurate
Talk about my fate with some programming languages
Hdu2377bus pass (build more complex diagram +spfa)
Duchefa丨D5124 MD5A 培养基中英文说明书
Influence of oscilloscope probe on measurement bandwidth
清除app data以及获取图标
Web Service简单入门示例
Chemical properties and application instructions of prosci Lag3 antibody
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)
Graph embedding learning notes
The development of research tourism practical education helps the development of cultural tourism industry








