Search code examples
javascriptwebstormjsdoc

Confusion of WebStorm type recognition of function return types


Using the following code:

/**
 * @typedef {foo} MyFoo
 * @property {String} fooText
 * @property {Number} fooValue
 */

/**
 * @type {MyFoo}
 */
var foo = {
  fooText : '',
  fooValue : 0
};

/**
 * @param {MyFoo} someFoo
 * @returns {MyFoo}
 */
function doStuff(someFoo)
{
  var result = Object.create(foo);

  result.fooText = someFoo.fooText + '!';
  result.fooValue = someFoo.fooValue + 1;

  return result; 
}


var someFoo = Object.create(foo);
someFoo.fooText = 'Hello';
someFoo.fooValue = 3;

var someOtherFoo = doStuff(someFoo);
var anotherFoo = doStuff(someOtherFoo);

I create a type MyFoo. I use this type as both the parameter and the return value of the function doStuff. Then, I repeatedly call the doStuff function, first with an object created with Object.create, then with the return value of the previous call.

Here is the issue: Argument type MyFoo is not assignable to parameter type MyFoo

Argument type MyFoo is not assignable to parameter type MyFoo

Plus, someOtherFoo and anotherFoo are not auto-completed with the MyFoo properties anymore.

What's happening?


Solution

  • It's a bug. WEB-13724 is fixed, fix will be included in WebStorm 10