当前位置:网站首页>ts中的联合类型和类型保护
ts中的联合类型和类型保护
2022-07-16 04:58:00 【aliven1】
interface Bird{
fly:boolean;
sing:()=>{
};
}
interface Dog{
fly:boolean;
bark:()=>{
};
}
// 1.类型断言的方式做类型保护
function trainAnimal(animal:Bird|Dog){
if(animal.fly){
(animal as Bird).sing();
}else{
(animal as Dog).bark();
}
}
// 2.in 语法来做类型保护
function trainAnimalSecond(animal:Bird|Dog){
if('sing' in animal){
animal.sing();
}else{
animal.bark();
}
}
//3.typeof 语法做类型保护
function add(first :string | number,second:string | number){
if(typeof first ==='string' || typeof second==='string'){
return `${
first}${
second}`;
}
return first+second;
}
// 4.使用instanceof 语法来做类型保护,下面用到了instanceof 所有只能用class,不能用///interface定义接口
class NumberObj{
count:number
}
function addSecond(first:object | NumberObj,second: object | NumberObj){
if(first instanceof NumberObj && second instanceof NumberObj){
return first.count+second.count;
};
return 0;
}
边栏推荐
- ES2022 Array.at( )
- Some useful web addresses
- $attrs is readonly $listenners is readonly 报错解决方法
- 二叉树的广度遍历
- Random vibration calculation and simulation verification of single degree of freedom system
- 【虚幻引擎UE】UE5动态PAK资源加载方法
- xxx.whl is not a supported wheel on this platfo解决办法
- npm与node版本不匹配 更新了npm 结果报错node和npm不匹配 怎么退回之前的npm 版本?或者说怎么查看node适配的npm版本
- 【深度学习-学习笔记01】
- 2018 Jiangsu Provincial Information and future programming expert competition test question -- (New) chicken and rabbit in the same cage
猜你喜欢

Abbyy finereader 15 standard OCR character recognition and PDF editing software tool
![[target tracking] image inter frame difference target detection based on background subtraction and MATLAB simulation](/img/50/b5f0bec4ea71a7f7df2e48eff61fcc.png)
[target tracking] image inter frame difference target detection based on background subtraction and MATLAB simulation

Cultural tourism night tour project helps the development of night economy

Chinese garbled code in kettle version 8.2

【虚幻引擎UE】打包异常问题合集

Filebeat collects kubernetes cluster logs

Dwelling apartment rental system based on jsp+servlet

Digital twin factory - Smart factory twin cockpit, realizing intelligent lean production management

用过useEffect,useLayoutEffect吗
![[JMeter] the Chinese display of JMeter response message is garbled](/img/06/98399233fbf576191fd594767a5ecc.png)
[JMeter] the Chinese display of JMeter response message is garbled
随机推荐
二叉树的深度遍历
Filebeat collects kubernetes cluster logs
160_ Skills_ Power Bi new function - calculate working days
preg_replace 代码执行漏洞之[BJDCTF2020]ZJCTF,不过如此
Ubuntu 18.04 install mysql5.7.35 with tar package
marginalization
MySQL的DML(数据操纵语言)
ping 命令还能这么玩?
Simulation volume leetcode [general] 1996 Number of weak characters in the game
Introduction to T100 interface development steps
XML文件删除掉注释
基于JSP+Servlet的蜗居公寓租赁系统
Idea merges dev branch code into master and so on
Feign 实现服务间并且调用时传递 header
Quarkus practice 2: develop rest ful services to realize simple curd functions
Differences between collections and collections
$attrs is readonly $listeners is readonly error reporting solution
PMP每日一练 | 考试不迷路-7.14
XML file delete comments
Basic knowledge of triode (Part 2) ②