2

Having a class defined as:

angular.module('trip')
        /** @class MyClass */
        .factory('MyClass', [Factory]);

function Factory() {
   return function(){
         // fields & functions
   }
}

Then in the code I would instantiate a new object:

/**
 * @param {MyClass} MyClass
 */
function(MyClass){
   var myClass = new MyClass(); // HERE MyClass is highlighted in phpStorm
}

When calling new MyClass(); the MyClass is highlighted in phpStorm and I get this warning:

Method expression is not of Function type

What does it mean? How to get rid of it?

3
  • A very similar question was asked here without any comment either: stackoverflow.com/q/32778278/1075289 Commented May 18, 2016 at 10:47
  • .factory('MyClass', Factory); (sans brackets)? Commented May 18, 2016 at 23:09
  • @GrahamPHeath The brackets are there because we use strict DI. So it ain't change anything. Commented May 19, 2016 at 6:15

1 Answer 1

10

WebStorm (along with other tools) understands /** @param {MyClass} MyClass */ as a declaration of an instance of MyClass. It says you can't instantiate an instance. I may only suggest to change JSDoc to /** @param {function():MyClass} MyClass */ to cease the warning. JSDoc itself doesn't offer a better way to express a factory.

Also, please take a look at https://youtrack.jetbrains.com/issue/WEB-17325.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx @de1mar, it's suppressing the warning and that's what I needed.
This answer is relevant for PhpStorm 2022.3 too, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.