代码之家  ›  专栏  ›  技术社区  ›  a-h

jsdoc和vscode:记录作为参数传递给另一个函数的函数

  •  9
  • a-h  · 技术社区  · 7 年前

    我看过jsdoc文档,其中建议使用 @callback 注释是必需的,但根据屏幕截图,Visual Studio代码(vscode)没有突出显示它。

    智能感知 location 参数显示其类型 any locator id 返回一个 Location ).

    shows the locater parameter not being type hinted

    class Location {
      constructor(position, count) {
        this.position = position;
        this.count = count;
      }
    }
    
    const items = {
      'USB Cable': new Location('Desk Drawer', 123),
      Keyboard: new Location('Desk Surface', 1),
    };
    
    /**
     * A locater.
     * @param {string} id 
     * @returns {Location}
     */
    const locaterA = id => items[id];
    
    /**
     * Finds the item by its unique id.
     * @callback locater
     * @param {string} id
     * @returns {Location}
     */
    
    /**
     * Attempt to find the item with the given locater.
     * @param {string} id 
     * @param {locater} locater
     */
    const locate = (id, locater) => locater(id);
    
    const result = locate('USB Cable', locaterA);
    
    console.log(result);
    

    这是我所做的工作的问题吗,vsdoc不支持用例,还是vscode不支持它?

    2 回复  |  直到 7 年前
        1
  •  7
  •   Alex    6 年前

    编辑 @callback 从TypeScript开始 2.9

    Vscode的IntelliSense不支持 @回调 . 正在此处跟踪: https://github.com/Microsoft/TypeScript/issues/7515 .

    作为一种方便的解决方法,您可以使用 @typedef :

    /**
     * Finds the item by its unique id.
     * @typedef {function(string): Location} Locater
     */
    
    /**
     * Attempt to find the item with the given locater.
     * @param {string} id 
     * @param {Locater} locater
     */
    const locate = (id, locater) => locater(id);
    

    enter image description here

        2
  •  3
  •   samanime    7 年前

    @callback : https://msdn.microsoft.com/en-us/library/mt162307.aspx

    我手边没有Visual Studio,但你可以试试谷歌闭包风格,就是这样做的:

    @param { function(string) : number } locator
    

    这表示它是一个接受字符串并返回数字的函数。

    https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler (搜索“函数返回类型”以跳转到相关部分)。

    我注意到,至少JetBrains支持这种语法。