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?
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.
Operator Name: Function new Type:
new
Syntax example: {function(new:goog.ui.Menu, string)}
{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'.
goog.ui.Menu
Defines the constructed constructor type.
Source: https://habr.com/ru/post/1481401/More articles:https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1481396/implementing-generic-repository-using-entity-framework-code-first&usg=ALkJrhhqwouJl4J7Jjsqe3-lQsJ4xENyKgHow to allow softkeyaybord to cover only certain types? - android-layoutQt Creator Unit Test Project - unit-testingWhat to use? WCF or sockets? in C # - c #Show / hide Soft Keyboard events in Android - androidHTML5 / Javascript validation using JAX-RS - restjava.io.IOException: Cannot start program "ant" - javaConvert Select2 input to tokens - jquery-select2Removing blank records from a Jasper report - jasper-reportsThe original Google Closure map is not linked to the source in Chrome - closuresAll Articles