当前位置:网站首页>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 .
边栏推荐
- 向日葵资深产品总监技术分享:“国民远控”如何在AD域环境下应用
- 实例搭建Flask服务(简易版)
- Machine learning based on deepchem
- (nowcoder22529C)dinner(容斥原理+排列组合)
- 力扣每日一题-第44天-205. 同构字符串
- three. JS Part 54 how to pass structure array to shader
- Matlab learning - accumulation of small knowledge points
- EMD empirical mode decomposition
- AI platform, AI midrange architecture
- Functions and comparison of repeaters, hubs, bridges, switches and routers
猜你喜欢

Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP

exness:鸽派决议帮助黄金反弹,焦点转向美国GDP

Uni app internationalization

ROS-Errror:Did you forget to specify generate_ messages(DEPENDENCIES ...)?

Ribbon principle analysis namedcontextfactory

RHCE的at,crontab的基本操作,chrony服务和对称加密和非对称加密

(2022 Hangdian multi school III) 1011 link is as bear (thinking + linear basis)

@Configuration (proxybeanmethods = false) what's the use of setting this to false

Batch production and upload sales NFT opensea eth polygon

How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency
随机推荐
数字孪生实际应用案例-智慧能源篇
Notes on letter symbol marking of papers
Shutter start white screen
Division of data link layer, protocols used in data link layer and detailed introduction
Common methods of lodash Library
three. JS Part 54 how to pass structure array to shader
Sleuth+zipkin to track distributed service links
向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
Sleuth+Zipkin 来进行分布式服务链路的追踪
Deep into C language (1) -- operators and expressions
(newcoder 15079) irrelevant (inclusion exclusion principle)
JS regular expression finds the number of times a character (string) appears (one line of code)
(2022杭电多校三)1002-Boss Rush(状压DP+二分)
5年多工作经验,工资给15k,要是你,你会接受吗?
Why do programmers so "dislike" the trunk development mode?
RTP send and receive h265
Tencent cloud logs in with PEM
Solve the problem of garbled code when opening the project code in idea
"The programming is not standardized, and my colleagues are in tears!"
Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP