简体   繁体   中英

JSDoc: @type for nested function

I have a factory function that defines and returns other function - myFunc . I want to document my working variables with JSDocs:

function factory() {
    function myFunc(param) {
        console.log(`Hello ${param}!`);
    }

    return myFunc;
}

/** @type {factory} */
const fact = factory;
/** @type {???} */
const fn = fact();
fn('world');

I can document factory function:

/** @type {factory} */
const fact = factory;

but my IDE (IDEA PhpStorm) cannot resolve inner function the same way:

/** @type {myFunc} */
const fn = factory();

I cannot use Ctrl+click navigation in this case. Does any method exist to address inner function? Smth. like factory#myFunc :

/** @type {factory#myFunc} */
const fn = factory();

It is possible with @exports directive on nested function:

function factory() {
    /** @exports myFunc */
    function myFunc(param) {
        console.log(`Hello ${param}!`);
    }
    return myFunc;
}

/** @type {factory} */
const fact = factory;
/** @type {myFunc} */
const fn = fact();
fn('world');

Your factory returns a function that takes a string and returns nothing.

This can be expressed with a type expression using the @return tag:

/** @return {function(string): void} */
function factory() {
  function myFunc(param) {
    console.log(`Hello ${param}!`);
  }
  return myFunc;
}

If x is assigned the result of factory() , your IDE should be able to work out its type:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM