ts基础
1 // tsconfig.json 项目中ts的配置文件 2 3 4 5 // 基本数据类型 和 any 6 var flag: boolean = true; 7 8 var num: number = 2; 9 10 var str: string = "abc"; 11 12 function add(): void { 13 console.log("pp"); 14 } 15 16 add(); 17 18 var anyThing: any = "122"; 19 anyThing = 123; 20 21 // 类型推断 22 var str1 = "dfsf"; 23 // str1 = 123 //会报错, 在ts中赋值的时候就已经指定类型 24 25 //联合类型 26 var handlebloe: string | number; 27 handlebloe = 1; 28 handlebloe = "erewr"; 29 // handlebloe = false //报错 30 31 function reduceFLA(row: string | number): string { 32 if (typeof row === "string") { 33 return ""; 34 } else { 35 return "1"; 36 } 37 } 38 39 reduceFLA("dfa"); 40 41 //定义数组 42 var arr1: number[] = [1, 2, 3]; 43 var arr2: Array<number> = [1, 2, 3]; 44 45 //定义接口 46 interface Isfilese { 47 path: string; 48 name: string; 49 old: number; 50 age?: number; 51 [propName: string]: any; // 任意属性 一个 interface 中任意属性只能有一个 52 } 53 54 var obj: Isfilese = { 55 path: "/", 56 name: "sheun", 57 old: 12, 58 }; 59 60 interface Isdeve { 61 name: string; 62 age: number; 63 height: number; 64 } 65 // 数组对象 接口结合犯行 66 var handlearr: Isdeve[] = [{ name: "fasd", age: 23, height: 32 }]; 67 68 var handlearr1: Array<Isdeve> = [{ name: "fasd", age: 23, height: 32 }]; 69 70 var handlearr2: Array<{ name: string; age: number }> = [ 71 { name: "fasd", age: 23 }, 72 ]; 73 74 //函数 function 输入输出 75 //声明式 和 函数表达式 76 77 function getDetail(name: string): void {} 78 79 getDetail("azs"); 80 81 // const getDetail1 = (row: Isdeve): Isdeve => { 82 // return row 83 // } 84 //等价于 和es6的箭头函数有区别 85 var getDetail1: (row: Isdeve) => number = (row: Isdeve): number => { 86 return 111; 87 }; 88 89 getDetail1({ name: "fasd", age: 23, height: 32 }); 90 91 // 用接口定义函数 92 interface IserachValue { 93 (name: string, age: number, height?: number): boolean 94 } 95 96 let Serachvalue: IserachValue = (name, age) => { 97 return true 98 } 99 100 Serachvalue("fasd", 23) 101 102 103 //类型断言 1. 值 as 类型 104 105 106 function getlist(row: string | number): number { 107 return (row as string).length 108 } 109 110 getlist('ewe') 111 112 //接口 定义函数时的两种方法 113 interface Issd { 114 name: string 115 run: () => void //1 116 } 117 118 interface Issad { 119 name: string 120 get(): void //2 121 } 122 123 function gerName(val: Issd | Issad): boolean { 124 if(typeof (val as Issd).run === 'function') { 125 (val as Issd).run() 126 return true 127 } else { 128 return false 129 } 130 } 131 var fasd: Issd = { 132 name: 'fds', 133 run() {console.log(1)}, 134 } 135 gerName(fasd) 136 137 138 //枚举 139 enum isStatus { 140 message, 141 age, 142 } 143 144 // 泛型 145 // 函数中泛型 146 function join<T>(first: T, second: T) { 147 return `${first}${second}` 148 } 149 150 join<number>(0, 2) 151 152 function muFun<T>(params: T) { 153 return params 154 } 155 156 muFun<string[]>(['sf', 'df']) 157 158 159 //接口中使用泛型 160 interface ISerachvalue { 161 <T>(name: T): T 162 } 163 164 var GetMyinfo: ISerachvalue = function <T>(name: T): T { 165 return name 166 } 167 168 GetMyinfo('shun') 169 170 var GetUsr:ISerachvalue = <T>(name: T): T => { 171 console.log(name) 172 return name 173 } 174 GetUsr('DFSD')