Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

Add global variables in typescript

In this example, we need to put placeholder values on global.window to allow us to use Ruby on Rails’ ActionCable websocket framework where no window exists:

// Fix to prevent crash from ActionCable
global.window.removeEventListener = () => {};
global.window.addEventListener = () => {};

But we need to add a type:

declare global {
    var window: {
        addEventListener(): void;
        removeEventListener(): void;
    };
}
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.