【发布时间】:2020-11-01 19:45:45
【问题描述】:
我想在 Typescript 和 React 中使用 forEach() 生成表行列表。使用下面的代码时,我得到Property 'forEach' does not exist on type。有谁知道如何解决这一问题?我不应该能够对 Person 使用 forEach(),因为它是 PersonInterfaces 数组吗?
interface PersonList {
[index: number]: PersonInterface
}
interface PersonInterface {
name: string,
age: number
}
const persons:PersonList = [
{
name: 'John',
age: 25,
}, {
name: 'Jill',
age: 28,
}
];
export default function Table() {
return (
<table>
{
persons.forEach(person => {
return (
<tr>
<td>{person.name}</td>
</tr>
);
})
}
</table>
);
}
【问题讨论】:
标签: reactjs typescript react-tsx