本文共 3798 字,大约阅读时间需要 12 分钟。
/** * 数据类型 */let s: string;let n: number;let b: boolean;// 数组let arr1: string[] = ['a', 'b', 'c'];let arr2: Array= [1, 2, 3, 4, 5];let tuple: [string, number] = ['a', 1]; // 元祖let arr3: (string|number)[] = ['a', 1];let anyList: any[] = ['a', 1, true];// 枚举enum Directions{ left=2, right, top, bottom}console.log(Directions.top);console.log(Directions[2]);// 对象interface LabelledValue { label: string;} function printLabel(labelledObj: LabelledValue) { console.log(labelledObj.label);} let myObj = {size: 10, label: "Size 10 Object"};printLabel(myObj);// 函数interface SearchFunc{ (source: string, subString: string): boolean}let mySearch: SearchFunc;mySearch = function(source:string, subString: string): boolean{ let result = source.search(subString); return result>-1;}// 其它方式interface Person { age:number } interface Musician extends Person { instrument:string } var drummer = {};
function add(x: number, y: number): number{ return x+y;}let myAdd = function(x: number, y: number): number{ return x+y;}// 函数的完整类型let myAdd_: (baseValue: number, increment: number) => number = function(x: number, y: number): number{ return x+y;}
class Person{ name: string; // 属性必须赋值或者在构造函数中初始化 age: number; constructor(name: string, age: number){ this.name = name; this.age = age; } // 方法必须实现 getName(): string{ return this.name; }}
abstract class Animal{ abstract type: string; // 抽象属性只需定义,无需实现(赋值或者在构造函数中初始化) name: string; // 普通属性定义同一般类 constructor(name: string){ this.name = name; } abstract getType(): string; // 抽象方法只需定义,无需实现 getName(): string{ // 普通方法定义同一般类 return this.name; } getTheType(): string{ return this.type; // 普通方法中可以包含抽象属性 }}class Dog extends Animal{ type: string; // 类中必须实现所继承的抽象类中的属性 constructor(name: string, type: string){ super(name); this.type = type; } // 类中必须实现所继承的抽象类中的方法 getType(): string{ return this.type; }}
// 属性接口interface LabelledValue { label: string;} function printLabel(labelledObj: LabelledValue) { console.log(labelledObj.label);} let myObj = {size: 10, label: "Size 10 Object"};printLabel(myObj);// 函数接口interface SearchFunc{ (source: string, subString: string): boolean}let mySearch: SearchFunc;mySearch = function(source:string, subString: string): boolean{ let result = source.search(subString); return result>-1;}// 可索引类型接口interface StringArray{ [index: number]: string;}let myArray: StringArray = ["Bob", "Fred"];let myStr: string = myArray[0];// 类接口interface Girl{ // 接口中的属性和方法只需定义,无需实现 name: string; age: number; loveYou():void;}class GrilFirend implements Girl{ // 接口中的属性和方法必须实现 name: string; age: number; constructor(name: string, age: number){ this.name = name; this.age = age; } loveYou():void{ console.log("Her name is " + this.name + ", her age is " + this.age); }}let gf = new GrilFirend("Linda", 28);gf.loveYou();
注意,无法创建泛型枚举和泛型命名空间。
类有两部分:静态部分和实例部分。 泛型类指的是实例部分的类型,所以类的静态属性不能使用这个泛型类型。
// 泛型函数function showData(person: T): T{ return person;}showData ('Lucy');// 泛型类型1interface GenericIdentityFn { (arg: T): T;}function identity (arg: T): T { return arg;}let myIdentity: GenericIdentityFn = identity;// 泛型类型2interface GenericIdentityFn { (arg: T): T;}function identity (arg: T): T { return arg;}let myIdentity: GenericIdentityFn = identity;// 泛型类// 类有两部分:静态部分和实例部分。 泛型类指的是实例部分的类型,所以类的静态属性不能使用这个泛型类型。class GenericNumber { zeroValue: T; add: (x: T, y: T) => T;}let myGenericNumber = new GenericNumber ();myGenericNumber.zeroValue = 0;myGenericNumber.add = function(x, y) { return x + y; };
转载地址:http://movq.baihongyu.com/