TypeScript 數據類型 //字元串 let str: string = "你好ts" let str1: string = "你好typescript" //布爾 var flag: boolean = true flag = false //數字 var a: number = 123 c ...
TypeScript---數據類型
//字元串 let str: string = "你好ts" let str1: string = "你好typescript" //布爾 var flag: boolean = true flag = false //數字 var a: number = 123 console.log(a); a = 12.41 console.log(a); //字元串 let str2: string = "this is ts" str2 = "哈哈哈" console.log(str2); //數字 let arr: number[] = [1234, 435] console.log(arr); //字元串 let arr1: string[] = ["psj", "sjh"] console.log(arr1); //數組寫法1 let arr3: Array<number> = [12, 34, 42] console.log(arr3); //數組寫法2 let arr4: Array<string> = ["string", "es6"] console.log(arr4); //元組類型(tuple) 數組的一種 let arr5: [string, number, boolean] = ["ts", 3.23, true] console.log(arr5); //枚舉類型(enum) enum Flag { success = 1, error = -1 } var f: Flag = Flag.success console.log(f); console.log(Flag.error); enum Color { red, blue = 10, yellow }; var c: Color = Color.blue; console.log(c); // 10 var c1: Color = Color.yellow; console.log(c1); //11
//null 空 var num4: number | null | undefined; num4 = 1234; console.log(num4); //void類型:TS中void表示沒有任何類型,一般用於定義方法的時候,方法沒有返回值 //es5 // function run() { // console.log('run'); // }; // run(); function run(): void { console.log('run'); //run } run(); //沒有返回任何類型 function run2(): number { return 123 }; run(); //never類型 var aa: never; aa = (() => { throw new Error("錯誤"); })()