【发布时间】:2021-11-05 12:42:21
【问题描述】:
- 我创建了一个元组数组
- 然后用
Object.fromEntries将其转换为对象 - 然后我将分配给
<{ [formFieldId: string]: number }>类型。
这里有什么问题?
const [itemsWithSelection, setItemsWithSelection] = useState<{ [formFieldId: string]: number }>(itemsWithSelection2)
if (!itemsWithSelection2) {
let aa = Object.keys(invoiceItems).forEach((key) =>
[key, 1]
)
setItemsWithSelection(Object.fromEntries(aa))
}
得到了这个错误:
./components/product/InvoiceItemsToDeliver.tsx:30:44
Type error: No overload matches this call.
Overload 1 of 2, '(entries: Iterable<readonly [PropertyKey, number]>): { [k: string]: number; }', gave the following error.
Argument of type 'void' is not assignable to parameter of type 'Iterable<readonly [PropertyKey, number]>'.
Overload 2 of 2, '(entries: Iterable<readonly any[]>): any', gave the following error.
Argument of type 'void' is not assignable to parameter of type 'Iterable<readonly any[]>'.
28 | [key, 1]
29 | )
> 30 | setItemsWithSelection(Object.fromEntries(aa))
| ^
31 | }
【问题讨论】:
-
forEach不返回任何内容 -aa始终为undefined。 -
我也试过
{return [key, 1]},结果一样 -
因为
.forEach()没有返回任何东西...
标签: typescript