Last active
June 25, 2017 09:18
-
-
Save markrendle/0c3876b0637f79802713 to your computer and use it in GitHub Desktop.
Add CustomEvent to lib.d.ts Window interface and global window object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
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; | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.dispatchEvent(new window.CustomEvent('Singularity')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I import a self-executing function like this polyfill.ts to my main.ts?