【发布时间】:2019-02-08 16:01:39
【问题描述】:
我正在尝试使用 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 存储库,它在项目的根目录中包含用于 tsc 设置的 tsconfig.json 文件:https://github.com/guyellis/typescript-as-a-linter
这是我从中获取上述代码的那个 repo 中的文件:https://github.com/guyellis/typescript-as-a-linter/blob/master/lib/reducer.js
【问题讨论】:
-
顺便说一句,你为什么使用
reduce而不是map的工作?const result = arr.map(item => [item, item.length]);. -
@ibrahimmahrir - 好问题。这是一个非常糟糕的例子!我应该提供一个示例来生成更大或更小的数组,而不是应该使用
map()。 -
我编辑了问题以使示例更加真实。
标签: javascript typescript jsdoc