Interfaces
TypeScript 的 type-checking 专注于值的“shape”
interfaces的作用在于命名值使其便于检测,同时作为软件与软件、软件内部交流的工具。
用于检测,编译成JS的话没有相应的语句
TS:
interface Squareconfig {
color: string;
width?: number;
}
function creatsqure(config:Squareconfig){color:string;area:number}{
//...
<pre>var newSquare = {color: "white", area: 100};
}
JS:
function creatsqure(config) { color: string; area: number; }
{
var newSquare = {color: "white", area: 100};
}
Function Type
interface FunctionExm{
(inData:number):boolean
}
var myFunctionExm:FunctionExm;
//...
Array Type
interface Dictionary {
[index: string]: string;//index为number或者string
//describe: string; 编译通过
}
Class Type
interface ClockInterface {
currentTime: Date;
setTime(d: Date);
}
class Clock implements ClockInterface {
currentTime: Date;
setTime(d: Date) {
this.currentTime = d;
}
constructor(h: number, m: number) { }
}
describe the public side of the class
Extend
<span style="font-size:12px;">interface Shape {
color: string;
}
interface PenStroke {
penWidth: number;
}
interface Square extends Shape, PenStroke {
sideLength: number;
}</span>
Hybird
interface Counter {
(start: number): string;
interval: number;
reset(): void;
}
var c: Counter;
c(10);
c.reset();
c.interval = 5.0;