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

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

    For those using the Angular CLI it's straightforward:

    src/polyfills.ts

    declare global {
      interface Window {
        myCustomFn: () => void;
      }
    }
    

    my-custom-utils.ts

    window.myCustomFn = function () {
      ...
    };
    

    If you're using IntelliJ, you also needed to change the following setting in the IDE before your new polyfills pick up:

    > File 
    > Settings 
    > Languages & Frameworks 
    > TypeScript 
    > check 'Use TypeScript Service'.
    

提交回复
热议问题