当前位置:网站首页>Typescript from getting started to mastering (XX) function generics
Typescript from getting started to mastering (XX) function generics
2022-07-29 03:44:00 【Haha, APE】
The function of generic
Let's start by writing a simple union type demo
To ensure that the method is available Let's use the string template to splice these two parameters
function join(first:string|number,second:string|number){
return `${
first}${
second}`
}
join(" ha-ha "," Ape ")
There is no problem with the code behind .
There is a new demand :first
This parameter is string
Type , second
For string
type , On the contrary, the same is true .
It is difficult to realize by using the knowledge we have learned before , So this time we need Generic
To solve this problem
Writing of function generics :
Angle brackets <T>
, In parentheses T
Any letter or word can be replaced Write the name you want to write , But it's better to be semantic , Generally speaking, we write by convention T
( Look professional )
function join<T>(first:T,second:T){
return `${
first}${
second}`
}
join<string>(" ha-ha "," Ape ")
When we adjust the time join
Function method , Use join< type >( Parameters )
( for example :join<string>(" ha-ha "," Ape ")
) Fill in the corresponding type in angle brackets
If you want both parameters to be number
So I could write it like this
join<number>(1,2)
Use of arrays in generics :
If an array is passed , We have two ways to define
- Add after generics
[]
- Add before generics
Array
// The first one is
function Monkey<T>(one:T[]){
return one
}
Monkey<string>([" ha-ha "," Ape "])
// The second kind
function Monkey<T>(one:Array<T>){
return one
}
Monkey<number>([1,2])
console.log(Monkey<number>([1,2]))
// Terminal output results :[ 1, 2 ]
Definition of multiple generics :
A function can define multiple generics
function join<T,P>(one:T,two:P){
return `${
one}${
two}`
}
join<string,number>(" I am haha ape ",23)
console.log(join<string,number>(" I am haha ape ",23))
Type inference for generics :
function join<T,P>(one:T,two:P){
return `${
one}${
two}`
}
join(" I am haha ape ",23)
In the above code, we are calling join Function method has no specified type , In fact, the program has inferred our type
Put the mouse on join(" I am haha ape ",23)
On this line of code , You will find the mystery
Be careful : We use type inference when using generics , It reduces the readability of the code . Using generics , It's best not to use type inference .
边栏推荐
- Why do programmers so "dislike" the trunk development mode?
- 1.5 nn. Module neural network (III)
- Why do many programmers hate pair programming?
- sql
- 3.1 common neural network layer (I) image correlation layer
- How to understand "page storage management scheme"
- 力扣每日一题-第44天-205. 同构字符串
- three.js 第五十四用如何给shader传递结构体数组
- Simple code implementation of decision tree
- (2022 Hangdian multi school III) 1002 boss rush (pressure dp+ dichotomy)
猜你喜欢
Machine learning based on deepchem
(codeforce547)C-Mike and Foam(质因子+容斥原理)
(2022杭电多校三)1011-Link is as bear(思维+线性基)
向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
Use of leak scanning (vulnerability scanning) tool burpsuite or burp Suite (with installation and installation package download of burpsuite+1.7.26)
The latest second edition of comic novels, listening to books, three in one, complete source code / integrated visa free interface / building tutorials / with acquisition interface
for_ Example of each usage
tron OUT_ OF_ ENERGY
Why does the 20 bit address bus determine the storage space of 1MB
Android view system and custom view Series 1: (kotlin version)
随机推荐
Why does the 20 bit address bus determine the storage space of 1MB
座机的表单验证
《陌路曾相逢》夏陌沈疏晏_夏陌沈疏晏最新章节
Tencent cloud logs in with PEM
Why do many programmers hate pair programming?
Android view system and custom view Series 1: (kotlin version)
SFTP upload error: com.jcraft.jsch JSchException: connection is closed by foreign host
Environment configuration stepping pit during colab use
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
Build redis environment under windows and Linux
CUB_200鸟类数据集关键点可视化
深入C语言(3)—— C的输入输出流
深入C语言(4)——switch的定义与使用
Deep into C language (1) -- operators and expressions
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
What you see and think in Microsoft
1.6 example: cifar-10 classification
How to realize shortcut keys for interface scaling by vscade
最新二开版漫画小说听书三合一完整源码/整合免签接口/搭建教程/带采集接口
The difference between int and integer. Is int or integer used in practical applications?