2016-04-22 37 views
0

我剛剛下載了一個JavaScript庫,名爲History.js如何在lib.d.ts中擴展聲明的變量?

它是javascript中的歷史對象的包裝來執行正確的跨平臺功能。

你可以像這樣在一個普通的Javascript環境中使用它,例如像這樣:History.pushState(data,title,url);

但我想在Typescript環境中使用它,這意味着我必須在d.ts文件中聲明函數/變量,但History變量已在默認lib.d.ts中聲明。

我可以看到它在這裏(10475行,第10485 & 16363):

interface History { 
    length: number; 
    state: any; 
    back(distance?: any): void; 
    forward(distance?: any): void; 
    go(delta?: any): void; 
    pushState(statedata: any, title?: string, url?: string): void; 
    replaceState(statedata: any, title?: string, url?: string): void; 
} 

declare var History: { 
    prototype: History; 
    new(): History; 
} 

declare var history: History; 

所以現在我的問題是如何利用History.js庫?我需要做什麼才能寫出「History.pushState(data,title,url);」在打字稿?

我相信我已經編輯了「申報VAR歷史:{...}」這個(糾正我,如果我錯了):

declare var History: { 
    prototype: History; 
    new(): History; 
    pushState(data: any, title: string, url: string): void; 
} 

而在這之後,我現在可以寫History.pushState(...);在我的Typescript代碼中。

但我不能保存lib.d.ts文件,因爲它受到保護。即使我可以保存它也不是那麼好,因爲那時我可以在其他可能沒有History.js的項目中使用這些函數,並且會出錯(我相信lib.d.ts是共享的項目)。

那麼我怎麼能夠做到這一點?

回答