JavaScript: defining custom global variable

Hello colleagues, 

 

In order to get rid of "Unknown Variable" warnings and to make development experience better, I want to set up the type definitions for custom global variables. I have the following scenario. I'm using Jest together with jest-puppeteer. I have successfully installed the type definitions for jest itself (through preferences -> Languages and Frameworks -> JavaScript -> Libraries) and warnings related to global Jest functions disappeared. However jest-puppeteer defines more globally accessible variables. One such variable is `page` that is central for testing with Puppeteer.  

The types for this global variable can be found together with other Puppeteer type declarations: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/puppeteer

However I could not find, how to configure one instance of `page` to be a global variable. I assume I need to define my own type definition file with that global variable and use it for WebStorm config, but I need some help on how to do that exactly. 

0
2 comments

>However I could not find, how to configure one instance of `page` to be a global variable.

haven't got what you mean, sorry... Try looking at existing d.ts files to see what global var definitions look like there. It can be

declare var varName

for example

2
Avatar
Permanently deleted user

Hi Elena, 

Thanks for your response, I was not sure how to add the global variable definitions as a library. Turned out I was missing one last step - adding (instead of downloading) a library and pointing to a local file with my custom type definitions. Now it works as a charm, thanks. 

Best regards,

Juriy

 

1

Please sign in to leave a comment.