代码之家  ›  专栏  ›  技术社区  ›  Guy

JavaScript Reducer Accumulator类型脚本的JSDoc类型

  •  2
  • Guy  · 技术社区  · 7 年前

    我试图在一个javascript中输入累加器 reducer 使用JSDocs,但无法解决如何执行此操作。

    我试过在初始化时以内联方式键入它,但没有成功。有什么提示或想法吗?这是示例代码。它抱怨传递给arr.push()的参数:

    /**
    * @type {Array<String>}
    */
    const arr = ['one', 'two', 'three'];
    
    /**
     * @type {Array<Array>}
     */
    const result = arr.reduce((acc, item) => {
       if(item.length % 3 === 0) {
         // [ts] Argument of type '(string | number)[]' is not assignable to
         // parameter of type 'never'.
         acc.push([item, item.length]);
       }
       return acc;
    }, []);
    

    这是Github回购 tsconfig.json TSC设置的项目根目录中的文件: https://github.com/guyellis/typescript-as-a-linter

    这是我从回购协议中获取上述代码的文件: https://github.com/guyellis/typescript-as-a-linter/blob/master/lib/reducer.js

    1 回复  |  直到 7 年前
        1
  •  2
  •   Matt McCutchen    7 年前

    never[] this thread

    /**
    * @type {Array<String>}
    */
    const arr = ['one', 'two', 'three'];
    
    /** @type {Array<[string, number]>} */
    const init = [];
    
    /**
     * @type {Array<[string, number]>}
     */
    const result = arr.reduce((acc, item) => {
       acc.push([item, item.length]);
       return acc;
    }, init);
    
    推荐文章