当前位置:网站首页>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
边栏推荐
- 爱普生L3153打印机如何清洗喷头
- 2022电工(初级)复训题库及在线模拟考试
- 文件的相对路径写法
- Tardigrade:Trino 解决 ETL 场景的方案
- 2022年R1快开门式压力容器操作特种作业证考试题库及答案
- 学习---有用的资源
- nn. Parameter and torch nn. Init series of functions to initialize model parameters
- Embedded DSP audio development
- Is it safe to buy stocks and open an account through the account opening link of the broker manager? Want to open an account for stock trading
- More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
猜你喜欢
【小程序】使用font-awesome字体图标的解决文案(图文)
如何获取GC(垃圾回收器)的STW(暂停)时间?
Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red
根据Explain查看sql执行计划,对SQL进行优化
matlab习题 —— 数据的基本处理
剑指 Offer 49. 丑数(三指针法)
如何编写简洁代码?(上)
建立自己的网站(17)
2022 operation of simulated examination platform of special operation certificate examination question bank for safety management personnel of hazardous chemical business units
2022安全员-C证考试题库模拟考试平台操作
随机推荐
导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
Is it better for a novice to open a securities account? Is it safe to open a stock trading account
[iptables & ICMP] description of ICMP Protocol in iptables default policy
More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
RichView TRVStyle
数的每一位平方和
【iptables&icmp】iptables默认策略中关于icmp协议的说明
数据库
Apache, IIS6 and ii7 independent IP hosts screen and intercept spider crawling (applicable to VPS virtual machine servers)
nn.Parameter和torch.nn.init系列函数给模型参数初始化
Etcd database source code analysis -- network layer server rafthandler between clusters
Simple elk configuration to realize production level log collection and query practice
Notepad++--常用的插件
【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘
Object类,以及__new__,__init__,__setattr__,__dict__
Is it safe to buy stocks and open an account through the account opening link of the broker manager? Want to open an account for stock trading
[522. longest special sequence II]
matlab习题 —— 矩阵的常规运算
collections.defaultdict()的使用
Dataloader parameter collate_ Use of FN