常用类型
-
Boolean
//Boolean类型定义 let isDone: boolean = false;
-
Number
//Number类型的定义,包括所有类型的数字,整数、小数、浮点数、十六进制数、八进制数、二进制数。 let decimal:number = 6; let hex:number = 0xf00d; let binary:number = 0b1010; let octal:number = 0o744;
-
String
//String类型的定义,'',""都可以, 多行时,可以用反引号``表示。 let color:string = "red"; color = 'blue'; let fullname:string = 'abby hua'; let age:number = 33; let sentence:string = `Hello, my name is ${fullname}. I will be ${age+1} years old next year. ` let sentence2:string = "Hello, my name is "+fullname+".\n\n"+"I will be "+(age+1)+" years old next year." document.body.innerHTML = sentence2; /* 输出结果: Hello, my name is abby hua. I will be 34 years old next year. */
-
Array
//Array 类型定义 //Array 定义1, elemType[] let list1: number[] = [1, 2, 3]; //Array 定义2, Array<elemType> let list2: Array<number> = [1, 2, 3]
-
Tuple
//Tuple 类型定义, tuple内的各元素类型已经确定,但类型可以不一致,比如string和number混合类型的情况 let student : [string, number]; student = ['abby', 30] //ok student = [30, 'abby'] //error console.log(student[0].substring(0)) // 可以用string的方法 student[2] = 'test' //Error, 给超出定义的index赋值,会报错
-
Enum
//Enum 类型定义,第一个元素默认从0开始,后面元素递加 enum Color {red, blue, yellow} let c: Color = Color.blue console.log(c) //输出blue的值1 //手动修改enum设置第一个元素值为1,则后面每个元素从1递加 enum Color1 {blue=1, red, yellow} let c1: Color1 = Color1.blue; //输出结果:1 let c2: Color1 = Color1.red; //输出结果:2 let c3: Color1 = Color1.yellow; //输出结果:3 //手动设置每个元素的值 enum Color2 {red=1, blue=3, yellow=5} let c4: Color2 = Color2.red; //输出结果:1 let c5: Color2 = Color2.blue; //输出结果:3 let c6: Color2 = Color2.yellow; //输出结果:5 let colorName: string = Color2[1] //red值为1, colorName为red console.log(c4) console.log(c5) console.log(c6) console.log(colorName)
-
Any
// Any 类型定义 // Any 可以表示任何类型 let notSure: any = 4; console.log(notSure); //输出结果:4 notSure = 'I am a string!'; //输出结果:I am a string! console.log(notSure); notSure = false; console.log(notSure); //输出结果:false //Any和Object区别. Any可以调用任意方法,Object不可以 notSure.ifItExists(); //okay, ifItExists might exist at runtime notSure.toFixed(); //okay, toFixed exists (but the compiler doesn't check) let prettySure: Object = 4; prettySure.toFixed();// Error: Property 'toFixed' doesn't exist on type 'Object'. //Any 也可用于数组元素类型不确定的情况 let anyArray: any[] = [1, 2, 3] anyArray[1] = 'teststring' console.log(anyArray) //输出结果:[1, "teststring", 3] let anyArray2: Array<any> = [4, 5, 6] anyArray2[0] = 'testtest' console.log(anyArray2) //输出结果: ["testtest", 5, 6]
-
Void
//Void 类型定义, 表示什么也不返回,常用于函数 function warnUser(): void{ console.log("This my warning message.") } warnUser(); //输出结果: This my warning message.
-
Null and Undefined
// Null and Undefined 类型定义 /* Null和Undefined是其它类型的子集,可以给其它类型赋值null or undefined. */ // Not much else we can assign to these variables! let u: undefined = undefined; let n: null = null; //变量可能是string, 可能是null,可能是undefined时,可以用string | null | undefined来表示 let str: string | null | undefined console.log(str) //定义成某一类型,不赋值直接用,vsc里面报错,输出undefined,类型定义成undefined, vsc里面不报错 let num: number; console.log(num) //输出undefined,vsc报错 let num: undefined; console.log(num) //输出undefined, vsc不报错
-
Never
- 是其它类型的子类型(包括null和undefined),代表从不会出现的值
- 意味着声明never类型的变量只能被never类型所赋值
let u: undefined = undefined; let n: null = null;