当前位置:网站首页>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
}
边栏推荐
- Ida Pro reverse tool finds the IP and port of the socket server
- Pat grade a 1103 integer factorizatio
- webgl_ Graphic transformation (rotation, translation, zoom)
- HW primary flow monitoring, what should we do
- Getting started with webgl (1)
- Oracle控制文件丢失恢复归档模式方法
- Use of SVN
- [quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
- 居然从408改考自命题!211华北电力大学(北京)
- Android -- jetpack: the difference between livedata setValue and postvalue
猜你喜欢

Getting started with webgl (1)
![[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset](/img/69/9dadeb92f8d6299250a894690c2845.png)
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset

Steps to create P8 certificate and warehousing account

Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
![[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice](/img/f7/03975d08912afd8daee936799e8951.png)
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice

How to create Apple Developer personal account P8 certificate

Use cpolar to build a business website (2)

webgl_ Graphic transformation (rotation, translation, zoom)
![[target detection] yolov5 Runtong voc2007 data set](/img/b3/b7f3d46075cb1782d772a24362003e.png)
[target detection] yolov5 Runtong voc2007 data set

使用cpolar建立一个商业网站(2)
随机推荐
Do not use memset to clear floating-point numbers
如何在opensea批量发布NFT(Rinkeby测试网)
webgl_ Graphic transformation (rotation, translation, zoom)
【OBS】RTMPSockBuf_ Fill, remote host closed connection.
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
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
Tkinter after how to refresh data and cancel refreshing
After UE4 is packaged, mesh has no material problem
Streaming end, server end, player end
Monthly observation of internet medical field in May 2022
Getting started with webgl (1)
2. Heap sort "hard to understand sort"
What are PV and UV? pv、uv
Cut ffmpeg as needed, and use emscripten to compile and run
Nacos一致性协议 CP/AP/JRaft/Distro协议
The rebound problem of using Scrollview in cocos Creator
LeetCode3_ Longest substring without duplicate characters
大表delete删数据导致数据库异常解决
Annexb and avcc are two methods of data segmentation in decoding
Summary of knowledge points of xlua hot update solution