当前位置:网站首页>函数的重载
函数的重载
2022-07-16 03:56:00 【要成为光的女人】
1.在TypeScript中,如果我们编写了一个add函数,希望可以对字符串和数字类型相加,应该如何编写呢?
//通过联合类型有两个缺点:
// 1.进行很多的逻辑判断(类型缩小)
//2.返回值的类型依然是不能确定
type AddType=number | string
function add(a1:AddType,a2:AddType){
return a1+a2
}2.函数的重载:
函数的重载:函数的名称相同,但是参数不同的几个函数,就是函数的重载
函数的执行是从上到下依次匹配的
function add(num1:number,num2:number):number;
function add(num1:string,num2:string):string;
function add(num1:any,num2:any):any{
return num1+num2
}
const result=add(20,30) //去匹配的时候一定是去匹配上面的某一个重载函数
边栏推荐
- ES (6/7/8/9/10/11) notes
- Halcon 3D create_ pose
- 动态炫酷的404页面源码
- 7.15 simulation summary
- Sword finger offer 63 Maximum profit of stock
- Dpdk flow filter summary (flow director/ rte_flow)
- How to choose a desktop multimeter?
- 28K monthly salary for software testing interview questions for large factories (you can take away the test paper with the permission of the interviewer)
- Work method record
- Go: about grpc
猜你喜欢

内网部署EDUSOHO遇到的问题

ORA-19625异常处理记录

Sword finger offer 10- ii Frog jumping on steps
![[software testing] 08 -- black box testing method (boundary value analysis method, cause and effect diagram and decision table method)](/img/36/81ca1f41222b59efcbafeeb5ad893c.jpg)
[software testing] 08 -- black box testing method (boundary value analysis method, cause and effect diagram and decision table method)

More than 130 enterprises and 3500 developers have jointly built the root community of opengauss open source database

ES (6/7/8/9/10/11) notes

Dual position relay dls-5/1

ACL Technology

leetcode - 面试题 02.05. 链表求和

Gan online learning notes
随机推荐
MySQL 高级原理: MySQL执行过程及执行顺序
6、 JMeter timer
『HarmonyOS』探索HarmonyOS应用
Rust中的函数function与方法method的区别
山东省中小企业数字化转型论坛成功举办,九州云赋能中小企业数智升级
Test logiciel exploratoire
OpenCvSharp (C# OpenCV) 指针方法读取和修改图像像素值(附源码)
Sword finger offer 42 Maximum sum of continuous subarrays
[notes] cryptography from introduction to earth | AES
Some points for attention in drawing excel charts
【软件测试】08 -- 黑盒测试方法(边界值分析法、因果图与决策表法)
7.15 Résumé du concours de simulation
layoutgan:generating graphic layouts with wiregrame discriminators
进程的状态
求水仙花数
How to customize an annotation?
[TinyML]APQ:Joint Search for Network Architecture, Pruning and Quantization Policy
[deep learning] environment configuration of hands-on learning and deep learning
QA robot section 1 - Introduction
潮玩华猫来袭丨继周杰伦联名款后 ,方文山携上链购再推“华流”顶流联名款公仔数藏