当前位置:网站首页>TypeScript 联合类型
TypeScript 联合类型
2022-06-28 02:38:00 【*且听风吟】
联合类型
联合类型(Union Types)可以通过管道(|)将变量设置多种类型,赋值时可以根据设置的类型来赋值。
语法:
Type1|Type2|Type3
实例:
let res: string|number
res = 12
console.log("数字为: "+ res)
res = "abcdefg"
console.log("字符串为: " + res)
编译成 js:
var res;
res = 12;
console.log("数字为: " + res);
res = "abcdefg";
console.log("字符串为: " + res);
如果赋值其他类型会报错:
script.ts(6,1): error TS2322: Type 'true' is not assignable to type 'string | number'.
也可以讲联合类型作为函数参数使用:
function show(str:string|string[]) {
if(typeof str == "string") {
console.log(str)
} else {
var i;
for(i = 0;i<str.length;i++) {
console.log(str[i])
}
}
}
show("string") // string
show(["abc","def","efg","hij"])
// abc
// def
// efg
// hij
编译成js:
function show(str) {
if (typeof str == "string") {
console.log(str);
}
else {
var i;
for (i = 0; i < str.length; i++) {
console.log(str[i]);
}
}
}
show("string"); // string
show(["abc", "def", "efg", "hij"]);
// abc
// def
// efg
// hij
联合类型数组
可以将数组声明为联合类型。
实例:
let arr:number[]|string[]
let i:number
arr = ["abc","def","efg","hij"]
for (let item of arr){
console.log(item)
}
// abc
// def
// efg
// hij
arr = [1, 2, 3, 4]
for (let item of arr){
console.log(item)
}
// 1
// 2
// 3
// 4
编译成js:
var arr;
var i;
arr = ["abc", "def", "efg", "hij"];
for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
var item = arr_1[_i];
console.log(item);
}
// abc
// def
// efg
// hij
arr = [1, 2, 3, 4];
for (var _a = 0, arr_2 = arr; _a < arr_2.length; _a++) {
var item = arr_2[_a];
console.log(item);
}
// 1
// 2
// 3
// 4
边栏推荐
- Tardigrade: Trino's solution to ETL scenarios
- 在excel文件上设置下拉选项
- Brief history and future trend of codeless software
- Dataloader参数collate_fn的使用
- The same is MB. Why is the gap so large?
- 导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
- Object类,以及__new__,__init__,__setattr__,__dict__
- Excel知识技能汇总
- ETCD数据库源码分析——集群间网络层服务端RaftHandler
- RichView TRVStyle TextStyles
猜你喜欢

Excel知识技能汇总

【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘

Etcd database source code analysis -- network layer server rafthandler between clusters

WebSocket(简单体验版)
![Redis cluster setup [simple]](/img/20/9974a290f8c5d346e2b404b48b02e5.png)
Redis cluster setup [simple]

Artifact for converting pcap to JSON file: joy (installation)

项目实战!手把手教你 Jmeter 性能测试

R1 Quick Open Pressure Vessel Operation Special Operation Certificate Examination Library and Answers in 2022

爱普生L3153打印机如何清洗喷头

2022电工(初级)复训题库及在线模拟考试
随机推荐
__getitem__和__setitem__
Is it better for a novice to open a securities account? Is it safe to open a stock trading account
Apache——阿帕奇简介
启牛商学院赠送证券账户是真的吗?开户到底安不安全呢
【522. 最长特殊序列 II】
Apache, IIS6, ii7 independent IP host shielding restricts IP access
WebSocket(简单体验版)
matlab习题 —— 数据的基本处理
MySQL错误
PPT制作小技巧
将PCAP转换为Json文件的神器:joy(安装篇)
剑指 Offer 47. 礼物的最大价值(DP)
Idea auto generate code
劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)
没错,是水的一篇
Basic operation of stack (implemented in C language)
根据Explain查看sql执行计划,对SQL进行优化
2022安全员-C证考试题库模拟考试平台操作
ETCD数据库源码分析——集群间网络层服务端RaftHandler
s32ds跳转到DefaultISR