当前位置:网站首页>Simple understanding and application of TS generics
Simple understanding and application of TS generics
2022-07-07 15:47:00 【Sam young】
What is generics
Simply speaking , Generic refers to a type in different places , There are different values , Listed below
function creatValue(a: string):string {
return a;
}
function creatValueNumber(a: number):number {
return a;
}
Or use any, But it will lose its use ts Type constraints of , Now you can solve this problem by using generics
function creatValue<T>(a: T):T {
return a;
}
creatValue<string>("string")
/** You can also directly pass in ellipsis <> */
creatValue("string")
...
Of course, it can also be made into multiple
function creatValue<T,X>(a: T,b:X):T {
console.log(b)
return a;
}
creatValue<string,number>("a",123)
// perhaps
creatValue("a",123)
Inherited usage of generics
/** Define basic information */
interface base {
name:string;
age:number;
}
/** Define a student type , Generic indeterminate types then inherit base */
interface student<T extends base> {
baseInfo: T
}
/** Here I customize the type I want */
interface myBase extends base {
address: string;
grade:string
}
/** Final type */
const studentA: student<myBase> ={
baseInfo: {
name:' Zhang San ',
age:14,
address:' China ',
grade:' The new moon '
}
}
/** Define another body type */
interface bodyBase extends base {
weight: string;
height:string
}
const studentB: student<bodyBase> ={
baseInfo: {
name:' Zhang San ',
age:14,
weight:'70kg',
height:'170cm'
}
}
/** Can also inherit all */
interface AllBase extends bodyBase,myBase {
}
const studentC: student<AllBase> ={
baseInfo: {
name:' Zhang San ',
age:14,
weight:'70kg',
height:'170cm',
address:' China ',
grade:' The new moon '
}
}
Hard core
interface base {
name:string;
age:number;
}
interface student<T extends base> extends baseInfo<T> {
test(t: T):T;
}
interface baseInfo<T> {
baseInfo: T
}
interface bodyInfo extends base {
weight: string;
height: string;
}
const studentA:student<bodyInfo> = {
baseInfo:{
name:' Zhang San ',
age:18,
weight:'70kg',
height:'170'
},
test(t: bodyInfo): bodyInfo{
return t
}
}
function test<T extends bodyInfo>(option: baseInfo<T>){
option.baseInfo.age = 1
}
边栏推荐
- Window环境下配置Mongodb数据库
- 【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
- webgl_ Graphic transformation (rotation, translation, zoom)
- [quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
- Create lib Library in keil and use lib Library
- What are PV and UV? pv、uv
- #HPDC智能基座人才发展峰会随笔
- The difference between full-time graduate students and part-time graduate students!
- 避坑:Sql中 in 和not in中有null值的情况说明
- What is Base64?
猜你喜欢
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
Cocos creator collision and collision callback do not take effect
如何在opensea批量发布NFT(Rinkeby测试网)
HW初级流量监控,到底该怎么做
After UE4 is packaged, mesh has no material problem
Streaming end, server end, player end
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
居然从408改考自命题!211华北电力大学(北京)
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
Unity's ASE achieves full screen sand blowing effect
随机推荐
【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
Annexb and avcc are two methods of data segmentation in decoding
unnamed prototyped parameters not allowed when body is present
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation
Oracle控制文件丢失恢复归档模式方法
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
MySQL bit type resolution
Use of SVN
【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
OpenGL common functions
Super signature principle (fully automated super signature) [Yun Xiaoduo]
webgl_ Enter the three-dimensional world (2)
C Alibaba cloud OSS file upload, download and other operations (unity is available)
How to deploy the super signature distribution platform system?
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
[original] all management without assessment is nonsense!
Unity之ASE实现全屏风沙效果
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code