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

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

    Just found the answer to this in another StackOverflow question's answer.

    declare global {
        interface Window { MyNamespace: any; }
    }
    
    window.MyNamespace = window.MyNamespace || {};
    

    Basically you need to extend the existing window interface to tell it about your new property.

提交回复
热议问题