1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
let arr: string[] = ["a", "b"];
let arr1: Array<string> = ["a", "b"];
let arr2: (number | string)[]; arr2 = ["a", 1, "b", 2];
interface Person { name: string, age: number } let arr3: Person[] = [{name: "alan", age: 10}];
function sum(x: number, y: number): number { return x + y; }
let mySum: (x: number, y: number)=>number; mySum = function(x: number, y: number) { return x + y; }
interface Isearch { (source: string, substring: string): boolean; }
let fn: Isearch; fn = function(x: string, y: string) { return true; }
function buildName(firstName: string = "hello", lastName?: string): string { if(lastName) { return firstName + " " + lastName; } else { return firstName; } } let tname = buildName("alan", "liang"); let tom = buildName();
`ps: 可选参数后面不允许再出现必须参数`
function push(arr: any[], ...items: any[]): void { items.forEach(function(item: any){ arr.push(item); }); } let arr: any[] = []; push(arr, 1, 2, 3); console.log(arr);
function add(x: any, y: any): number { return x + y; } console.log(add(1, 3)); console.log(add("a", "b"));
type Combinable = string | number;
function add1(x: Combinable, y: Combinable): Combinable { if(typeof x === 'string' || typeof y === "string") { return x.toString() + y.toString() } return x + y; } console.log(add1(1, 3)); console.log(add1("a", "b")); let result: Combinable = add1("a", "b");
type Types = number | string; function addNum(a: number, b: number): number; function addNum(a: string, b: string): string; function addNum(a: number, b: string): string; function addNum(a: string, b: number): string; function addNum(a: Types, b: Types): Types { if(typeof a === 'string' || typeof b === "string") { return a.toString() + b.toString(); } return a + b; }; let res = addNum('apple', 'banana'); res.split("");
let x: [string, number]; x = ["a", 1];
let employee: [number, string] = [1, "alan"]; let [id, username] = employee; console.log(`id: ${id}`); console.log(`username: ${username}`);
let employee1: [number, string] = [1, "alan"];
let optionalTuple: [string, number?]; optionalTuple = ["alan", 2]; console.log(optionalTuple); optionalTuple = ["alan"]; console.log(optionalTuple);
type Point = [number, number?, number?]; const x: Point = [10]; const y: Point = [20, 21]; const z: Point = [30, 31, 32];
type RestTupleType = [number, ...string[]]; let restTuple: RestTupleType = [2, "apple", "banana", "pineapple"]; console.log(restTuple[0]); console.log(restTuple[1]);
const point: readonly [number, number] = [10, 20];
let a: void; let b: number = a;
function fn(): undefined {
}
function err(msg: string): never { throw new Error(msg); } function loopForever(): never { while(true) {} }
let ne: never; let nev: never = (()=> {throw new Error("err")})(); let an: any;
ne = nev;
type Foo = string | number; function withNever(foo: Foo) { if(typeof foo === "string") { } else if(typeof foo === "number") { } else { const check: never = foo; } }
let a: any = 10; a = "alan"; a = false; a = undefined; a = null; a = []; a = {};
let anyThing: any = "hello"; console.log(anyThing.myName);
let any2: any = "world"; any2.setName("alan");
let something; something = "something";
let unk: unknown = 2; let uncertain: any = unk; let un: unknown = unk;
function hello() { return "abc"; } const dog: unknown = { hello: hello};
function world() { let x: unknown; return x; }
const x = world(); const word = x.toLowerCase();
if(typeof x === "string") { const x1 = x.toLowerCase(); }
const y = (x as string).toLowerCase();
let num: number = 10; let Num: Number = 20;
Num = num; num = Num;
let lowerObj: object; lowerObj = 1; lowerObj = "a"; lowerObj = true; lowerObj = undefined; lowerObj = null; lowerObj = {};
let upperObj: Object; upperObj = 1; upperObj = "a"; upperObj = true; upperObj = undefined; upperObj = null; upperObj = {};
type objlowerExtendsObj = object extends Object ? true : false; type ObjExtendsLowerobj = Object extends object ? true : false; let lowerCaseObject: object; let upperCaseObject: Object={}; lowerCaseObject = upperCaseObject; upperCaseObject = lowerCaseObject;
|