let e:unknown; // unkonwn // 必须使用 类型断言缩小未知范围 才能被赋值 // 或 // 使用类型收缩 如 typeof 或 instanceof // https://blog.csdn.net/azl397985856/article/details/104470902 e = 'hello'; let s:string; // 断言 s = e as string; s = <string>e; //never 无返回值 function fn2(): never{ throw new Error("报错了!"); } // {指定对象内包含哪些属性} ?: 可选 let b: {name: string, age?: number, [propName: string]: any}; b = {name: "mike", "gender": 18} let f: (a: number, b: number)=>number; f = function (n1: number, n2: number): number{ return n1 + n2; } let arr: string[]; // let arr: Array<string>; // 元组 固定长度数组 let h: [string, string]; h = ['hello', 'world']; // enum 枚举 enum Gender{ Male, Female } let i: {gender: Gender}; i = { gender: Gender.Male } // &表示同时 let j: string & number; // let j1: { name: string } & { age: number }; j1 = {name: "mike", age: 18}; // 类型别名 let k: 1 | 2 | 3 | 4 | 5; k = 5; type myType = string; let m:myType;
x