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

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

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

window.MyNamespace = window.MyNamespace || {};
23条回答
  •  萌比男神i
    2020-11-22 04:25

    If you need to extend the window object with a custom type that requires the use of import you can use the following method:

    window.d.ts

    import MyInterface from './MyInterface';
    
    declare global {
        interface Window {
            propName: MyInterface
        }
    }
    

    See 'Global Augmentation' in the 'Declaration Merging' section of the Handbook: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation

提交回复
热议问题