当前位置:网站首页>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')
边栏推荐
- 重上吹麻滩——段芝堂创始人翟立冬游记
- 珍爱网微服务底层框架演进从开源组件封装到自研
- How to renew NPDP? Here comes the operation guide!
- 示波器探头对测量带宽的影响
- Abnova CD81 monoclonal antibody related parameters and Applications
- 驱动壳美国测试UL 2043 符合要求有哪些?
- LeetCode: Distinct Subsequences [115]
- Abnova丨血液总核酸纯化试剂盒预装相关说明书
- Monorepo管理方法论和依赖安全
- Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
猜你喜欢
ClickHouse 复制粘贴多行sql语句报错
浅聊我和一些编程语言的缘分
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions
PVC 塑料片BS 476-6 火焰传播性能测定
Duchefa low melting point agarose PPC Chinese and English instructions
Abnova丨DNA 标记高质量控制测试方案
Graph embedding learning notes
PHP deserialization +md5 collision
木板ISO 5660-1 热量释放速率摸底测试
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
随机推荐
Duchefa丨D5124 MD5A 培养基中英文说明书
判断横竖屏的最佳实现
phpstudy小皮的mysql点击启动后迅速闪退,已解决
教你自己训练的pytorch模型转caffe(一)
LeetCode: Distinct Subsequences [115]
PHP反序列化+MD5碰撞
matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)
How to make ERP inventory accounts of chemical enterprises more accurate
Write an interface based on flask
MySQL ifnull usage function
Monorepo管理方法论和依赖安全
LeetCode: Distinct Subsequences [115]
The reason why the ncnn converted model on raspberry pie 4B always crashes when called
ClickHouse 复制粘贴多行sql语句报错
Abnova fluorescent dye 620-m streptavidin scheme
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
Interpreting the daily application functions of cooperative robots
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
当Steam教育进入个性化信息技术课程
WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid