Google closure: how to comment on a parameter used as a constructor

I use require.js to manage Google Closure dependencies and annotations to give me autocomplete (in WebStorm / IDEA). We rely heavily on Backbone and develop our Java-style Javascript code, i.e. Both with static and with instances.

What remains elusive is how to write annotations for function arguments that are actually classes โ€” parameters used with the new keyword.

require(['foo'], function(Foo) { var bar = new Foo(); }); 

WebStorm and / or Google Closure correctly assume that this is an instance of Foo if I annotate the parameter as follows:

 require(['foo'], /** @param {Foo} Foo */ function(Foo) { Foo.<cursor> <-- gives me an autocompletion for an instance of Foo }); 

A quick google search} suggests using the function (new: Foo) as a parameter description. However, this approach loses autocomplete for constructor parameters and / or possible static methods that the class has.

The design I hope should look like this:

 require(['foo'], /** @param {Type<Foo>} Foo */ function(Foo) { Foo.<cursor> <-- gives me an autocompletion for statics of Foo }); 

Is there any way to achieve this?

+4
source share
1 answer

You are looking for

 /** @param {function(new: Foo)} someConstructor */ 

You can also specify constructor arguments by doing something like

 function(new: Foo, ArgType1, ArgType2) 

Relevant excerpt from docs :

Operator Name: Function new Type:

Syntax example: {function(new:goog.ui.Menu, string)}

A function that takes a single parameter (string) and creates a new instance of goog.ui.Menu when called with the keyword 'new'.

Defines the constructed constructor type.

+7
source

Source: https://habr.com/ru/post/1481401/


All Articles