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

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

    I wanted to use this in an Angular (6) library today and it took me a while to get this to work as expected.

    In order for my library to use declarations I had to use the d.ts extention for the file that declares the new properties of the global object.

    So in the end, the file ended up with something like:

    /path-to-angular-workspace/angular-workspace/projects/angular-library/src/globals.d.ts

    Once created, don't forget to expose it in your public_api.ts.

    That did it for me. Hope this helps.

提交回复
热议问题