当前位置:网站首页>TypeScript基础类型
TypeScript基础类型
2022-06-28 05:11:00 【阿嚏阿嚏-】
TypeScript支持JavaScript的基本数据类型,还提供了实用的枚举类型
- boolean,string,null,undefined,number
/** * 数值类型 */
let num: number = 1
console.log(num);
console.log('==============');
/** * 字符串 */
let str: string = '字符串'
console.log(str);
console.log('==============');
/** * 布尔 */
let bool: boolean = true
console.log(bool);
console.log('==============');
//总结: ts中变量一开始定义为什么类型,就只能赋值该类型,不容许赋其他类型的值
/** * null和undefined */
let nul: null = null
let und:undefined = undefined
console.log(nul,und);
//undefined和null都可以作为其他类型的子类型,即可以将undefined和null赋值给其他类型的变量
let num2: number = 2
num2 = null
console.log('num2',num2);
console.log('==============');
- 数组类型
/** * 数组类型 */
// 数组定义方式1: let 变量名:数据类型[] = [值1,值2,。..]
let arr1: number[] = [10,20,30]
console.log(arr1);
// 数组定义方式2,数组泛型: let 变量名:Array<数据类型> = [值1,值2,。..]
let arr2: Array<number> = [100,200,300]
let srtArr: Array<string> = ['1','2','3']
console.log(arr2);
console.log(srtArr);
console.log('==============');
// 注意:数组定义后,值类型必须和定义数组时定义的类型一致
- 元组类型Tuple
元组类型允许表示一个已知元素数据类型和数量的数组,各元素类型不必相同
/** * 元组类型 */
let arr3: [boolean,string,number] = [true,'小甜甜',100.12345]
console.log(arr3);
console.log(arr3[1].split(''));
console.log(arr3[2].toFixed(2));
// 注意:元组类型在使用的时候,数据的类型、位置和个数与元组定义时元素的类型、位置和个数相对应
- 枚举
有一组数据比较常用,且个数固定,就可以定义为枚举类型。枚举类型是ts中对js的基本数据类型的补充,使用枚举可以对一组数据赋予友好的名字
//枚举里的每个数据值都可以叫元素,每个元素都是有编号的,编号依次递增。
//若不给枚举中的元素赋值,则值为编号,若赋值则为赋的值,且其他元素的编号也会变为赋值后再递增的值
enum Color {
red,
blue=10,
green
}
let red: Color = Color.red
console.log(red);
console.log(Color.red, Color.blue, Color.green);
console.log(Color);
//可以通过编号获取到枚举类型中元素的数值
console.log(Color[10]);
//枚举里元素可以是中文数值,但不推荐
- any类型
any类型定义一个任意类型的值
let temp: any = 100
temp = 'any类型'
console.log(temp);
// 当一个数组中要存储多个类型不确定,个数不确定的数据时,也可以使用any数组
let anyArr: any[] = [100, '1', {
name: '小红', age: 1}, true]
console.log(anyArr);
// 总结:any类型的有点在于可以存储不确定类型和个数的数据,但使用错误语法也不会报错,例如:console.log(anyArr[0].splice(''));
- void类型
void类型与any类型相反,它表示没有任何类型,当函数没有返回值时,则其返回值类型为void
//声明一个没有任何返回值的函数
function showMsg(): void{
console.log('这是个没有返回值的函数');
// return
// return undefined
return null
}
console.log(showMsg());
//定义void类型的变量,可以接受一个undefined或null的值
let vd: void = undefined
console.log(vd);
- object类型,非原始类型
//定义一个函数,参数是object类型,返回值也是object类型
function getObj(obj: object): object{
console.log(obj);
return {
name: '大明',
age: 27
}
}
// console.log(getObj({name: '二明',age: 25}));
// console.log(getObj(new String('123')));
console.log(getObj(String));
- 联合类型和类型断言
联合类型表示取值可以为多种类型的一种
类型断言: 告诉编辑器,知道这这个变量是个什么类型的值,也知道在干什么
// 1. 定义一个函数,接收一个Number类型或String类型的参数,得到字符串形式的值
// function getStr(str:number|string): string{
// return str.toString()
// }
//2. 定义一个函数,接收一个Number类型或String类型的参数,得到字符串值的长度
/** * 类型断言 */
//语法方式1: <类型>变量名
//语法方式2: 变量名 as 类型
function getStrLen(str: number | string): number{
if((<string>str).length) {
return (str as string).length
}else {
return str.toString().length
}
}
console.log(getStrLen(1234));
console.log(getStrLen('123456'));
- 类型推断
ts会在定义变量没有明确的指定变量时推测出一个类型
//1. 定义变量时赋值了,推断为赋值的类型
let text = '呜呜呜'
//2. 定义变量时没赋值,推断为any类型
let x;
边栏推荐
- 改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP
- Gorm transaction experience
- MySQL export query results to excel file
- Performance degradation during dpdk source code testing
- 交流电和直流电的区别是什么?
- cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结
- Is it enough for the project manager to finish the PMP? no, it isn't!
- 电源插座是如何传输电的?困扰小伙伴这么多年的简单问题
- msa. h: There is no such file or directory
- Hundreds of lines of code to implement a script interpreter
猜你喜欢
MySQL 45讲 | 05 深入浅出索引(下)
The heading angle of sliceplane is the same as that of math Corresponding transformation relation of atan2 (y, x)
A guide to P2P network penetration (stun) for metartc5.0 programming
电源插座是如何传输电的?困扰小伙伴这么多年的简单问题
2022 safety officer-b certificate examination question bank and answers
二级造价工程师证书含金量到底有多高?看这些就知道了
Leetcode 88: merge two ordered arrays
Gorm transaction experience
How to design an awesome high concurrency architecture from scratch (recommended Collection)
IP datagram sending and forwarding process
随机推荐
How to learn programmable logic controller (PLC)?
How long will the PMP test results come out? You must know this!
PMP考试成绩多久出来?这些你务必知道!
Steve Jobs' speech at Stanford University -- follow your heart
BioVendor sRAGE蛋白解决方案
Interview: what are the similarities and differences between abstract classes and interfaces?
2022烟花爆竹经营单位安全管理人员特种作业证考试题库及模拟考试
8VC Venture Cup 2017 - Elimination Round D. PolandBall and Polygon
【JVM系列】JVM调优
When excel copies the contents of a row, the columns are separated by the tab "\t"
Biovendor sRAGE antibody solution
2022 Western pastry (Advanced) test question simulation test platform operation
2022年材料员-通用基础(材料员)操作证考试题库及答案
[untitled] drv8825 stepping motor drive board schematic diagram
Understanding the source of innovation II
2022 safety officer-b certificate examination question bank and answers
[JVM] - memory partition in JVM
【牛客网刷题系列 之 Verilog快速入门】~ 四选一多路器
刘海屏手机在部分页面通过[[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom得到底部安全区高度为0问题
Store inventory management system source code