Search code examples
javascriptintellij-ideaphpstormjsdoc

Specify type of globals declared in another file


How can I tell my IDE (PHPStorm) that certain global variables aren't "undeclared", but simply declared elsewhere; and have a specific type?

An example from Magento (opcheckout.js):

    if (response.duplicateBillingInfo) {
        shipping.setSameAsBilling(true); // "shipping undeclared" warning
    }
        // in fact, shipping is a global variable with constructor "Shipping".

What I'd like to do is something like this:

        /** @var Shipping window.shipping */
        shipping.setS // with autocompletion:
                setSameAsBilling

Solution

  • A first part of answer - how to mark them as externally declared:

    Put a normal var declaration to the beginning of "importing" file.

    This (a bit surprisingly) doesn't replace 'global' with 'module local' because JS doesn't have module local variables.

    For the second part - I'm using PyCharm and it seems like handling such cases quite fine, at least in 2.0 EAP.