- 布尔类型 boolean
- 数字类型 number
- 字符串类型 string
- 数组类型 array
- 元祖类型 tuple
- 枚举类型 enum
- 任意类型 any
- null和undefined
- void 类型
- nerver类型 ```typescript // 布尔类型 boolean true false let falg: boolean = true; flag = false;
// 数组类型 number let num: number = 123; let num1: number = 1.23; console.log(num, num1) // 123, 1.23 let nums: number = ‘123’; console.log(nums); //提示类型错误
// 字符串写法 string let str = “hello word”; console.log(str); // hello word
// 数组写法 array 两种写法 // 第一种 let arr: number[] = [123,1,2,3]; let arr1: string[] = [‘a’, ‘b’, ‘c’]; console.log(arr, arr1); //123,1,2,3 a,b,c
// 第二种
let arr:Array
// 元祖 属于数组的一种 let arr:[boolean, number, string] = [true, 1, ‘a’]; console.log(true, 1, a);
// 枚举类型 enum enum Flag {success = 1, error = -1}; let types: Flag = Flag.success; console.log(types) // 1
enum color {orange, yellow, blue}; //未赋值的话打印出的值是索引值 let colour:color = color.yellow; console.log(colour); // 1
enum color {orange, yellow = 5, blue}; let colour1:color = color.orange; let colour2:color = color.yellow; let colour3:color = color.blue; console.log(colour1,colour2,colour3); // 0, 5, 6
// 任意类型 any let arbitrarily1: any = 123; let arbitrarily2: boolean = true; let arbitrarily3: string = ‘123’; let arbitrarily4: number = 123; let arbitrarily5: number[] = [1,2,3,4,5]; let Box: any = document.getElementById(‘box’); box.style.color = ‘red’;
// null 和undefined 其他数据类型的子类型
let num: undefined; console.log(num); // undefined let num1: number | undefined; num1 = 123; console.log(num1); // 123 num1; console.log(num1); // undefined
let nu: null; nu = null; console.log(nu); // null
// void类型 表示没有任何类型, 一般定义方法的时候方法没有返回值 function run(): void {}; run(); // 表示该方法没有返回值
// 有返回值 function run(): number { return 123 }; run();
// never类型 包括null和undefined的子类型,代表从不会出现的值, 也就是说never的变量值能被never类型所赋值 let a: undefined; a = 123; // 错误 a = undefined; //正确
let b: null; b = 123; // 错误 b = null; // 正确
let fn: never; fn=(() => { throw new Error(‘错误’); })(); ```
