当前位置:网站首页>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;
边栏推荐
- JS 文本框失去焦点修改全半角文字和符号
- Assembly common instructions
- Have you finished the examination of level II cost engineer? There are also qualification regulations!
- 如何从零设计一款牛逼的高并发架构(建议收藏)
- Redis 的 最新windows 版本 5.0.14
- msa. h: There is no such file or directory
- The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022
- A guide to P2P network penetration (stun) for metartc5.0 programming
- Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism
- Informatics Orsay all in one 1360: strange lift
猜你喜欢

Don't roll! How to reproduce a paper with high quality?

IP datagram sending and forwarding process

Lumiprobe细胞成像分析:PKH26 细胞膜标记试剂盒

2022 low voltage electrician examination questions and answers

Excel将一行的内容进行复制时,列与列之间是用制表符“\t”进行分隔的

The short video local life section has become popular. How to grasp the new opportunities?

SlicePlane的Heading角度与Math.atan2(y,x)的对应转换关系

Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism

2022新版nft源码中国元宇宙数字藏品艺术品交易平台源码

gorm事务体验
随机推荐
Latest Windows version 5.0.14 of redis
Gorm transaction experience
quartus 复制IP核
cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结
DH parameters of robotics and derivation using MATLAB symbolic operation
The heading angle of sliceplane is the same as that of math Corresponding transformation relation of atan2 (y, x)
分享一个因子挖掘的利器:遗传规划
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
Carboxylic acid study: lumiprobe sulfoacyanine 7 dicarboxylic acid
Excel将一行的内容进行复制时,列与列之间是用制表符“\t”进行分隔的
When using the MessageBox of class toplevel, a problem pops up in the window.
程序员-放羊娃
When excel copies the contents of a row, the columns are separated by the tab "\t"
禁用右击、键盘打开控制台事件
乔布斯在斯坦福大学的演讲稿——Follow your heart
[leetcode] 12. Integer to Roman numeral
What are functions in C language? What is the difference between functions in programming and functions in mathematics? Understanding functions in programming languages
吴恩达深度学习测验题:deeplearning.ai-week1-quiz
摄像头基础知识
Qcom LCD commissioning