接口 Interfaces
第一个接口 Our First Interface
interface LabelledValue {
label: string;
}
function printLabel(labelledObj: LabelledValue) {
console.log(labelledObj.label);
}
var myObj = {label: "only label"};
var myObj1 = {size: 10, label: "Size 10 Object"};
printLabel(myObj);
printLabel(myObj1);
可选属性 Optional Properties
interface SquareConfig {
color?: string;
width?: number;
}
function createSquare(config: SquareConfig): {color: string; area: number} {
var newSquare = {color: "white", area: 100};
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}
var mySquare = createSquare({color: "black"});
var mySquare1 = createSquare({width: 20});
console.log(mySquare);
console.log(mySquare1);
函数类型 Function Types
interface SearchFunc {
(source: string, subString: string): boolean;
}
var mySearch: SearchFunc;
mySearch = function(source: string, subString: string): boolean {
var result = source.search(subString);
if (result == -1) {
return false;
}
else {
return true;
}
};
var result: boolean = mySearch("test", "te");
console.log(result);
数组类型 Array Types
interface StringArray {
[index: number]: string;
}
var myArray: StringArray;
myArray = ["Bob", "Fred"];
console.log(myArray["0"]);
console.log(myArray["1"]);
类类型 Class Types
interface ClockStatic {
new (hour: number, minute: number);
}
class Clock {
currentTime: Date;
constructor(h: number, m: number) { }
}
var cs: ClockStatic = Clock;
var newClock: ClockStatic = new cs(7, 30);
console.log(newClock);
继承接口 Extending Interfaces
interface Shape {
color: string;
}
interface PenStroke {
penWidth: number;
}
interface Square extends Shape, PenStroke {
sideLength: number;
}
var square = {};
square.color = "blue";
square.sideLength = 10;
square.penWidth = 5.0;
混合类型 Hybrid Types
interface Counter {
(start: number): string;
interval: number;
reset(): void;
}
var c: Counter;
c(10);
c.reset();
c.interval = 5.0;
类 Classes
类 Classes
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet(): string {
return "Hello, " + this.greeting;
}
}
var greeter = new Greeter("world");
var result = greeter.greet();
console.log(result);
继承 Inheritance
class Animal {
name:string;
constructor(theName: string) { this.name = theName; }
move(meters: number = 0) {
console.log(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
constructor(name: string) { super(name); }
move(meters = 5) {
console.log("Slithering...");
super.move(meters);
}
}
class Horse extends Animal {
constructor(name: string) { super(name); }
move(meters = 45) {
console.log("Galloping...");
super.move(meters);
}
}
var sam = new Snake("Sammy the Python");
var tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
公/私 修饰器 Private/Public modifiers
默认公有 Public by default
class Animal {
private name:string;
constructor(theName: string) { this.name = theName; }
move(meters: number) {
alert(this.name + " moved " + meters + "m.");
}
}
理解私有 Understanding private
class Animal {
private name:string;
constructor(theName: string) { this.name = theName; }
}
class Rhino extends Animal {
constructor() { super("Rhino"); }
}
class Employee {
private name:string;
constructor(theName: string) { this.name = theName; }
}
var animal = new Animal("Goat");
var rhino = new Rhino();
var employee = new Employee("Bob");
animal = rhino;
class A {
public name: string;
echo() {}
}
class B {
public name: string;
echo() {}
}
var a = new A();
var b = new B();
a = b;
参数属性 Parameter properties
class Animal {
constructor(private name: string) { }
move(meters: number) {
console.log(this.name + " moved " + meters + "m.");
}
}
var dog = new Animal("dog");
dog.move(3);
访问器 Accessors
var passcode = "secret passcode";
class Employee {
private _fullName: string;
get fullName(): string {
return this._fullName;
}
set fullName(newName: string) {
if (passcode && passcode == "secret passcode") {
this._fullName = newName;
}
else {
console.log("Error: Unauthorized update of employee!");
}
}
}
var employee = new Employee();
employee.fullName = "Bob Smith";
if (employee.fullName) {
console.log(employee.fullName);
}
静态属性 Static Properties
class Grid ;
calculateDistanceFromOrigin(point: )
constructor (public scale: number)
}
var grid1 = new Grid(1.0);
var grid2 = new Grid(5.0);
console.log(grid1.calculateDistanceFromOrigin());
console.log(grid2.calculateDistanceFromOrigin());
Advanced Techniques
模块 Modules
函数 Functions
泛型 Generics
异常 Common Errors
Mixins
Declaration Merging
Type Inference
Type Compatibility
Writing .d.ts files