How do you explicitly set a new property on `window` in TypeScript?

后端 未结 23 2637
青春惊慌失措
青春惊慌失措 2020-11-22 03:53

I setup global namespaces for my objects by explicitly setting a property on window.

window.MyNamespace = window.MyNamespace || {};
23条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 04:38

    The accepted answer is what I used to use, but with TypeScript 0.9.* it no longer works. The new definition of the Window interface seems to completely replace the built-in definition, instead of augmenting it.

    I have taken to doing this instead:

    interface MyWindow extends Window {
        myFunction(): void;
    }
    
    declare var window: MyWindow;
    

    UPDATE: With TypeScript 0.9.5 the accepted answer is working again.

提交回复
热议问题