当前位置:网站首页>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
边栏推荐
- 栈的基本操作(C语言实现)
- 在excel文件上设置下拉选项
- 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
- Arm development studio build compilation error
- 项目实战!手把手教你 Jmeter 性能测试
- kubernetes资源对象介绍及常用命令
- 开口式霍尔电流传感器如何助力直流配电改造?
- Go 数据类型篇(四)之浮点型与复数类型
- Apache - Introduction à Apache
- Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red
猜你喜欢

基于 LNMP 搭建个人网站的填坑之旅

荣耀v8 真机调试时不显示 Logcat 日志的解决办法

Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red

Idea auto generate code

service实现类里面为何一直报红

Summary of the use of composition API in the project

CI & CD 不可不知!

嵌入式软件开发中必备软件工具

17 `bs object Node name h3 Parent ` parents get parent node ancestor node

2022安全员-C证考试题库模拟考试平台操作
随机推荐
2022安全员-C证考试题库模拟考试平台操作
物体上下漂浮工具
INFO:&nbsp;HHH000397:&nbsp;Using…
Go speed
2022危险化学品经营单位安全管理人员特种作业证考试题库模拟考试平台操作
17 `bs object Node name h3 Parent ` parents get parent node ancestor node
2022 electrician (elementary) recurrent training question bank and online simulation examination
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
__getitem__和__setitem__
database
数据库乱码问题
MySQL 数据库的自动备份操作
数据库系列之MySQL中的分页查询优化
A pit filling trip based on LNMP to build a personal website
Win10 如何删除系统盘大文件hiberfil.sys
Websocket (simple experience version)
2022 safety officer-c certificate examination question bank simulated examination platform operation
学习---有用的资源
开口式霍尔电流传感器如何助力直流配电改造?
GAMES104 作业2-ColorGrading