TypeScript学习笔记二:基本类型

1、任意类型:any

声明为 any 的变量可以赋予任意类型的值。

let test: any = null;

2、数字类型:number

二进制、八进制、十进制、十六进制数

let test: number = 6666;

3、字符串:string

单引号或双引号或反引号(`)来表示

let test: string = "Hello World";

4、布尔类型:boolean

let test: boolean = true;

5、数组类型:[]

在元素类型后加上[]

let test: number[] = [1, 3, 4];

或使用数组泛型

let test2: Arry<number> = [1, 2, 3];

6、元组

元组类型用来表示已知元素数量和类型的数组,各元素的类型不必相同,对应位置的类型需要相同。

let test: [string, number] = ["hello-world", 666];

7、枚举:enum

定义数值集合

enum Color {Red, Green, Blue};
let c: Color = Color.Blue;
console.log(c);    // 输出 2

异构枚举

enum color {
	red = '测试red',
	green = '测试green',
	blue = 6}
let a: color = color.red;
let b: color = color.green;
let c: color = color.blue
console.log(a,b,c)

8、void

用于标识方法返回值的类型,表示该方法没有返回值。

function hello(): void{
	console.log('hello world)
}

9、null

表示对象值缺失

10、undefined

用于初始化变量为一个未定义的值

11、never

never 是其他类型(包括 null 和 undefined)的子类型,代表从不会出现的值。

发布了142 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_35958891/article/details/104434271