//还要注意的是,在构造函数的参数上使用public等同于创建了同名的成员变量。 class Student { fullName: string; constructor(public firstName, public middleInitial, public lastName) { //4个成员变量 this.fullName = firstName + " " + middleInitial + " " + lastName; } } interface p{ firstName:string; middleName:string; lastName:string; fullName:string; } function a(s:p){ return "aa" + s.firstName + s.middleName + s.lastName + s.fullName; } var s = new Student('fff','mmm','lll'); console.log(a(s)); ------------------------------------------------------------------------------------------- 元组类型: var x: [string, number] = ['hello', 10]; var x = ["sss",222]; var x : string|HTMLElement; class Dog {woof(){}} class Cat {meow(){}} var pet: Dog|Cat; ------------------------------------------------------- 联合类型; interface p{ name:string; age:string | string[] | ( ()=>string ); } var p1 : p = {name:"sss",age:["sdss","dee"]}; console.log(p1.name + p1.age); -------------------------------------------------------- 联合类型|泛型 function cc<T>(x:T,y:T):T{ //申明时不指定参数类型 return Math.random() > 0.5 ? x : y; } console.log( cc<string>('1','3') ); //调用的时候确定参数类型 console.log( cc<string|numnber>(1,'3') ); function ss<T,M>(x:T,y:M):T|M { return Math.random() > 0.5 ? x : y; } console.log( ss<string,number>("www",22) ); --------------------------------------------------------- 模版字符串: var name = "TypeScript"; var greeting = `Hello, ${name}! Your name has ${name.length} characters`; 当编译目标为ES6之前的版本时,这个字符串被分解为: var name = "TypeScript!"; var greeting = "Hello, " + name + "! Your name has " + name.length + " characters"; --------------------------------------------------------- 类型别名: type PrimitiveArray = Array<string|number|boolean>; type MyNumber = number; type NgScope = ng.IScope; type Callback = () => void; ---------------------------------------------------------- const enum(完全嵌入的枚举) const enum Suit { Clubs, Diamonds, Hearts, Spades } var d = Suit.Diamonds; Compiles to exactly: var d = 1; ---------------------------------------------------------- 解构声明会引入一个或多个命名变量, 并且初始化它们的值为对象的属性或者数组的元素对应的值. 比如说, 下面的例子声明了变量 x, y 和 z, 并且分别将它们的值初始化为 getSomeObject().x, getSomeObject().x 和 getSomeObject().z; var { x, y, z } = getSomeObject(); 交换2个值 var x = 1; var y = 2; [x, y] = [y, x]; -------------------------------------------------------- never类型是任何类型的子类型,也可以赋值给任何类型;然而,没有类型是never的子类型或可以赋值给never类型(除了never本身之外)。 即使 any也不可以赋值给never。 // 返回never的函数必须存在无法达到的终点 function error(message: string): never { throw new Error(message); } ---------------------------------------------------------- 类型断言好比其它语言里的类型转换, let someValue: any = "this is a string"; let strLength: number = (<string>someValue).length; 另一个为as语法: let someValue: any = "this is a string"; let strLength: number = (someValue as string).length; ---------------------------------------------------------------- 函数参数也使用函数作用域。 解构: 数组的解构赋值: let input = [1, 2]; let [first, second] = input; console.log(first); // outputs 1 console.log(second); // outputs 2 // swap variables [first, second] = [second, first]; --------------------------------------- function f([x,y]:[number,number]){ console.log(x); console.log(y); } let i:[number,number] = [3,4]; f(i); -----------------------------------------------------------------