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

后端 未结 23 2224
青春惊慌失措
青春惊慌失措 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:19

    Make a custom interface extends the Window and add your custom property as optional.

    Then, let the customWindow that use the custom interface, but valued with the original window.

    It's worked with the typescript@3.1.3.

    interface ICustomWindow extends Window {
      MyNamespace?: any
    }
    
    const customWindow:ICustomWindow = window;
    
    customWindow.MyNamespace = customWindow.MyNamespace {} 
    

提交回复
热议问题