当前位置:网站首页>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')
边栏推荐
- Maker education infiltrating the transformation of maker spirit and culture
- Abnova 环孢素A单克隆抗体,及其研究工具
- 使用WebAssembly在浏览器端操作Excel
- Duchefa cytokinin dihydrozeatin (DHZ) instructions
- Influence of oscilloscope probe on measurement bandwidth
- 驱动壳美国测试UL 2043 符合要求有哪些?
- ts 之 属性的修饰符public、private、protect
- Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
- Is the securities account given by the school of Finance and business safe? Can I open an account?
- How to open an account online for futures? Is it safe?
猜你喜欢
ClickHouse 复制粘贴多行sql语句报错
Abnova fluorescent dye 620-m streptavidin scheme
重上吹麻滩——段芝堂创始人翟立冬游记
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
Duchefa丨MS培养基含维生素说明书
教你自己训练的pytorch模型转caffe(三)
ProSci LAG3抗体的化学性质和应用说明
Duchefa丨P1001植物琼脂中英文说明书
Duchefa丨D5124 MD5A 培养基中英文说明书
PHP deserialization +md5 collision
随机推荐
Enclosed please find. Net Maui's latest learning resources
PHP反序列化+MD5碰撞
AITM 2-0003 水平燃烧试验
序列联配Sequence Alignment
渗透创客精神文化转化的创客教育
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
Write an interface based on flask
When steam education enters personalized information technology courses
Material Design组件 - 使用BottomSheet展现扩展内容(二)
ClickHouse 复制粘贴多行sql语句报错
Determine the best implementation of horizontal and vertical screens
Hdu2377bus pass (build more complex diagram +spfa)
Careercup its 1.8 serial shift includes problems
leetcode:1139. 最大的以 1 为边界的正方形
POJ 3414 pots (bfs+ clues)
sql系列(基础)-第二章 限制和排序数据
请查收.NET MAUI 的最新学习资源
水泥胶黏剂BS 476-4 不燃性测试
Traps in the explode function in PHP
bazel是否有学习的必要