2012-12-19 125 views

回答

7

我不認爲你可以延長時間有一個額外的靜態屬性。可以按如下方式擴展其原型:

interface Date { 
    min: Date; 
} 

Date.prototype.min = new Date(); 

var x = new Date(); 
alert(x.min.toString()); 

做你真正想做的事情,你居然就必須做出改變,以lib.d.ts:

declare var Date: { 
    new(): Date; 
    new (value: number): Date; 
    new (value: string): Date; 
    new (year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date; 
    (): string; 
    prototype: Date; 
    parse(s: string): number; 
    UTC(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number; 
    now(): number; 
    min: Date; 
} 

並進行擴展在純JS中,除了TypeScript生成的JavaScript之外,還要加載它。

Date.min = new Date(); 
+0

是否有任何其他方式來定義靜態屬性?而無需更改lib.d.ts. –

+1

我會彈出一個答案在這裏:http://stackoverflow.com/questions/13955157/how-to-define-static-property-in-typescript-interface – Fenton