Skip to content

Instantly share code, notes, and snippets.

@markrendle
Last active June 25, 2017 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markrendle/0c3876b0637f79802713 to your computer and use it in GitHub Desktop.
Save markrendle/0c3876b0637f79802713 to your computer and use it in GitHub Desktop.
Add CustomEvent to lib.d.ts Window interface and global window object
interface CustomEventParams {
bubbles?: boolean;
cancelable?: boolean;
detail?: any;
}
// Extend the lib.d.ts CustomEvent interface with the proper constructor
interface CustomEvent {
new(event: string, params?: CustomEventParams);
}
// Add the interface to the Window interface
interface Window {
CustomEvent: CustomEvent;
}
(() => {
function polyfill(pf: any) {
window.CustomEvent = pf;
}
polyfill((event, params) => {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = <CustomEvent>document.createEvent('CustomEvent');
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return evt;
});
})();
window.dispatchEvent(new window.CustomEvent('Singularity'));
@Rested
Copy link

Rested commented Jun 25, 2017

How do I import a self-executing function like this polyfill.ts to my main.ts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment