JavaScript 减速器累加器的打字稿的 JSDoc 类型

JSDoc typings for typescript for JavaScript reducer accumulator

我正在尝试使用 JSDocs 在 JavaScript reducer 中键入累加器,但不知道如何执行此操作。

我试过在初始化时直接输入它,但没有用。任何提示或想法?这是示例代码。它抱怨传递给 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

这是我从中获取上面代码的那个 repo 中的文件:https://github.com/guyellis/typescript-as-a-linter/blob/master/lib/reducer.js

您作为初始状态传递的空数组获得类型 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);
/**
 * @param {Pose['parameterValues']} values
 * @param {Pose['variants']} variants
*/
function getSrc (values, variants) {
  return values.reduce(
    /** @param {{[key: string]:Array<string>}} paths */
    (paths, { camera, light }, index) => {
      paths[light.name] = []
      for (let c = camera.start; c < camera.end; c += camera.increment) {
        paths[light.name].push(
          variants[index].replace('light', light.value).replace('camera', String(c))
        )
      }
      return paths
    }, {})
}