当前位置:网站首页>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
}
边栏推荐
- MongoD管理数据库的方法介绍
- 大表delete删数据导致数据库异常解决
- When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
- jacoco代码覆盖率
- Clang compile link ffmpeg FAQ
- Nacos一致性协议 CP/AP/JRaft/Distro协议
- Cut ffmpeg as needed, and use emscripten to compile and run
- Wechat applet 01
- 有钱人买房就是不一样
- What are PV and UV? pv、uv
猜你喜欢
The difference between full-time graduate students and part-time graduate students!
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
Asynchronous application of generator function
【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
Whole process analysis of unity3d rendering pipeline
Create lib Library in keil and use lib Library
Monthly observation of internet medical field in May 2022
The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
随机推荐
What are the safest securities trading apps
2022 all open source enterprise card issuing network repair short website and other bugs_ 2022 enterprise level multi merchant card issuing platform source code
LeetCode3_ Longest substring without duplicate characters
Webcodecs parameter settings -avc1.42e01e meaning
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
MongoDB数据库基础知识整理
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
避坑:Sql中 in 和not in中有null值的情况说明
Steps to create P8 certificate and warehousing account
【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)
MySQL bit类型解析
LeetCode1_ Sum of two numbers
【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
2.Golang基础知识
webgl_ Enter the three-dimensional world (1)
【兰州大学】考研初试复试资料分享
STM32F103C8T6 PWM驱动舵机(SG90)
Annexb and avcc are two methods of data segmentation in decoding