1
我發現this文章其相當有趣,但它不幫助我,當我嘗試擴展像window
這樣的全局變量時。將屬性添加到打字稿中的javascript對象而沒有錯誤
Test.ts
window.test = {}; //ERROR: Property 'test' does not exist on type 'Window'.
(function (test)
{
//do stuff
} (window.test)); //Build: Property 'test' does not exist on type 'Window'
錯誤消息:
ERROR: Property 'test' does not exist on type 'Window'.
我該如何解決這個問題?
有沒有像JS中的'interface'或它只是在打字稿? –
@MahmoudZakal'interface'是一個打字稿的東西:http://www.typescriptlang.org/docs/handbook/interfaces.html,因爲在javascript中不需要這樣的東西 –
截至今天,還有點兒需要在JS中執行此操作,如果您使用TS編譯器使用'checkJs'選項進行類型檢查。在這種情況下,編譯器會抱怨'window.test = ...',但用window [「test」] = ...'可以。 – Coderer