How to tell WebStorm the type of "this"

I am using the code inspector, along with jsdoc comments, to try to clean up my code.

I have some issues, because I haven't yet found any good documentation on jsDoc and how WebStorm code inspector uses it (pointers welcome).

I have the following code:

        function makeForm(selector, options) {
            //...
            $('body').on('change', 'mytable :input', function() {
                // Inspector complains "Argument type makeForm is not assignable to parameter type jElement"
            });
            //...
        }


        /**
         * inputValue function
         * @param {jElement} field
         * @returns {string}
         */
        function inputValue(field) {
            //...
        }


How do I tell WebStorm that the this parameter in the function is of type jElement?
5 comments
Comment actions Permalink

Please can you provide the complete code snippet? I can understand nothing from your example.

0
Comment actions Permalink

Sorry, I left out the crucial function call:

        function makeForm(selector, options) {
            //...
            $('body').on('change', 'mytable :input', function() {
                inputValue(this); // Inspector complains "Argument type makeForm is not assignable to parameter type jElement"
            });
            //...
        }


        /**
         * inputValue function
         * @param {jElement} field
         * @returns {string}
         */
        function inputValue(field) {
            //...


        }
0
Comment actions Permalink

The simplest workaround is to prefix callback with JSDoc comment having @this tag

$('body').on('change', 'mytable :input', /** @this jElement */ function() {
  inputValue(this);
});


I've created WEB-18976 for it to work without @this tag, as it seems it should.

0
Comment actions Permalink

Thankyou Konstantin.

Were can I find documentation on the JSDoc tags supported by WebStorm, and what they do?

0
Comment actions Permalink

There is no such place specifically for WebStorm, but you may refer to usejsdoc.org and google closure compiler docs, we are trying to follow them.

0

Please sign in to leave a comment.