TypeScript——基础数据类型

1
2
// typescript基础数据类型
// Boolean, Number, String, Array, Enum(枚举), Any, void

定义布尔类型

1
var isBoon:boolean = false;

定义数字类型

1
var num:number = 10;

定义字符串类型

1
var str:string = 'adsf';

定义数组类型

1
2
var list1:number[] = [1, 2, 3];
var list2:Array<number> = [4, 5, 6];

定义枚举

1
2
3
// 定义角标
enum Color {Red = 1, Green, Blue};
var colorName:string = Color[2]; // Green

1
2
3
// 所有的项,都定义角标
enum Color {Red = 1, Green = 15, Blue = 20};
var colorName:string = Color[15]; // Green
1
2
3
返回角标
enum Color {Red = 1, Green = 15, Blue = 20};
var c:Color = Color.Green; // 15

定义任意类型

1
2
3
var notSure:any = "123";
notSure = 123;
notSure = false;

1
2
var list:any[] = [1, '2', true];
var list:Array<any> = [1, '2', true];