JSDoc structures with and Immutable.js in annotations

I am returning an Immutable.js List data structure from a function.

PHPStorm automatically adds the following @returns {*|List<T>|List<any>} .

Eslint gives me a warning An unresolved variable of type T. Where can I find documentation for annotations for Immutable.js?

How can I describe the @returnurn List annotation form that will be hosted in Eslint?

 /** * @param n * @returns {*|List<T>|List<any>} */ const getList = (n) => { let list = Immutable.List() for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { list = list.push(Immutable.List.of(i, j)) } } return list } 
+4
source share
1 answer

Although I am not familiar with Immutable.js, the problem is that T is a template that should be defined in your documentation. See that your function really returns, this is a list from a list of numbers. So T allowed by List<Number> , and your fixed documentation will look something like this:

 /** * @param {Number} n * @return {List<List<Number>>} */ 

And you can just get rid of * and List<any> as possible return types, since your function explicitly always returns a list of lists of numbers.

What is it.


About algorithmic complexity

Note that you wrote a function whose processing time increases quadratically with parameter n . If you often call a function that passes the same value, consider memoizing its return value:

 const memoizedLists = new Map(); /** * @param {Number} n * @return {List<List<Number>>} */ function getList(n) { // try to find a previous run for this same value let result = memoizedLists.get(n); if (!result) { // compute it otherwise result = Immutable.List(); for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { result.push(Immutable.List.of(i, j)); } } // memoize it for a future invocation memoizedLists.set(n, result); } return result; } 

In addition, not only time increases quadratically, but also memory use. Depending on how you use it, you probably want to make your function a generator function, which will “magically” make it use constant space, i.e. No matter how large n , your function will continue to use just the same amount of memory. Here, your function has turned into a generator function:

 /** * @generator * @param {Number} n * @yields {List<Number>} */ function *getList(n) { for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { yield Immutable.List.of(i, j); } } } 

To use it as a generator, you need to call him on demand. For example, if you print these pairs of numbers to some output:

 for (const pair of getList(4)) { console.info(`...and here comes another pair: [${pair}]`); } 
+3
source

All Articles