当前位置:网站首页>Typescript union type
Typescript union type
2022-06-28 03:35:00 【* listen to the wind】
Joint type
Joint type (Union Types) You can go through the pipes (|) Set variables to multiple types , The assignment can be made according to the type of setting .
grammar :
Type1|Type2|Type3
example :
let res: string|number
res = 12
console.log(" The number is : "+ res)
res = "abcdefg"
console.log(" String is : " + res)
Translate it into js:
var res;
res = 12;
console.log(" The number is : " + res);
res = "abcdefg";
console.log(" String is : " + res);
If you assign other types, an error will be reported :
script.ts(6,1): error TS2322: Type 'true' is not assignable to type 'string | number'.
You can also use union types as function parameters :
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
Translate it into 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
Union type array
You can declare an array as a union type .
example :
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
Translate it into 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
边栏推荐
- Change of monitoring documents and folders of "operation and maintenance department"
- 2022 operation of simulated examination platform of special operation certificate examination question bank for safety management personnel of hazardous chemical business units
- SSH框架的搭建(下)
- Necessary software tools in embedded software development
- mysql获取当前时间是一年的第多少天
- Inference optimization implementation of tensorrt model
- Go 數據類型篇(四)之浮點型與複數類型
- JS clear the object and its value:
- Is it better for a novice to open a securities account? Is it safe to open a stock trading account
- WPF 下的自定义控件以及 Grid 中控件的自适应
猜你喜欢
随机推荐
剑指 Offer 47. 礼物的最大价值(DP)
crond BAD FILE MODE /etc/cron. d
剑指 Offer 49. 丑数(三指针法)
The same is MB. Why is the gap so large?
资源管理、高可用与自动化(下)
[iptables & ICMP] description of ICMP Protocol in iptables default policy
WARN:&nbsp;SQL&nbsp;Error:&nbsp;…
__ getitem__ And__ setitem__
More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
Necessary software tools in embedded software development
What are the good practices of cloud cost optimization?
失联修复:让“躲猫猫”无处可藏
用于 C# 的 SQL 基本语法总结
SSH框架的搭建(上)
Brief history and future trend of codeless software
ARM Development Studio build编译报错
CURDATE()和NOW()区别
Establishment of SSH Framework (Part I)
Dataloader参数collate_fn的使用
A pit filling trip based on LNMP to build a personal website







![Redis cluster setup [simple]](/img/20/9974a290f8c5d346e2b404b48b02e5.png)

