规则
var 变量名 : 数据类型 = 初始值
TypeScript的数据类型包含:Boolean,Number,String,Array,Enum,Any,Void:
Boolean
var isDone: boolean = false;
真假值/布尔值,用来定义yes or no,true or false;
Number
var height: number = 6;
数字类型,用来定义数值数据,根据JavaScript的经验,最长为32位数据,待验证;
String
var name: string = "bob";
name = 'smith';
字符串类型,用来定义文本数据,可以拼接;
Array
var list: number[] = [1, 2, 3];
or
var list:Array<number> = [1, 2, 3];
数组数据,定义多个数据的集合;可以根据位置进行复制与获取值,可以进行遍历;
Enum
enum Color {Red, Green, Blue};
var c: Color = Color.Green;
or
enum Color {Red = 1, Green, Blue};
var c: Color = Color.Green;
or
enum Color {Red = 1, Green = 2, Blue = 4};
var c: Color = Color.Green;
or
enum Color {Red = 1, Green, Blue};
var colorName: string = Color[2];
alert(colorName);
枚举类型,可以定义每个枚举类型的初始值,可以定义不同的初始值,可以是步进值;
Any
var notSure: any = 4;
notSure = "maybe a string instead";
notSure = false;//okay, definitely a boolean
or
var list:any[] = [1, true, "free"];
list[1] = 100;
泛类型,相当于java的object,Objective-C的id,能赋值为任意数据类型。用于程序运行前不确定数据类型的位置;
Void
function warnUser(): void {
alert("This is my warning message");
}
空类型,目测是返回值,待验证