当前位置:网站首页>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;
边栏推荐
- How to learn programmable logic controller (PLC)?
- Learning Tai Chi Maker - mqtt Chapter II (VI) mqtt wills
- 氨基染料研究:Lumiprobe FAM 胺,6-异构体
- What is the difference between AC and DC?
- SlicePlane的Heading角度与Math.atan2(y,x)的对应转换关系
- Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
- MySQL export query results to excel file
- Reactive dye research: lumiprobe af594 NHS ester, 5-isomer
- [microservices openfeign] openfeign quick start service invocation based on feign
- It is the latest weapon to cross the blockade. It is one of the fastest ladders.
猜你喜欢
随机推荐
Simple usage of GSAP
cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结
[leetcode] 12. Integer to Roman numeral
程序员坐牢了,会被安排去写代码吗?
Understanding the source of innovation II
How to do a good job of gateway high availability protection in the big promotion scenario
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
Latest Windows version 5.0.14 of redis
Opencv实现颜色检测
Informatics Orsay all in one 1360: strange lift
Don't roll! How to reproduce a paper with high quality?
How does the power outlet transmit electricity? Simple problems that have plagued my little friend for so many years
【LeetCode】12、整数转罗马数字
【JVM系列】JVM调优
The heading angle of sliceplane is the same as that of math Corresponding transformation relation of atan2 (y, x)
高通平台 Camera 之 MCLK 配置
DPDK 源码测试时性能下降问题
Feign implements path escape through custom annotations
Store inventory management system source code
Have you finished the examination of level II cost engineer? There are also qualification regulations!









