Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 财产';forEach&x27;在Typescript中的XXX类型上不存在_Reactjs_Typescript_React Tsx - Fatal编程技术网

Reactjs 财产';forEach&x27;在Typescript中的XXX类型上不存在

Reactjs 财产';forEach&x27;在Typescript中的XXX类型上不存在,reactjs,typescript,react-tsx,Reactjs,Typescript,React Tsx,我想通过在Typescript中使用forEach()生成一个表行列表,并做出反应。使用下面的代码时,I get属性“forEach”在类型上不存在。有人知道如何解决这个问题吗?我不应该在persons上使用forEach(),因为它是PersonInterface数组吗 interface PersonList { [index: number]: PersonInterface } interface PersonInterface { name: string, age:

我想通过在Typescript中使用
forEach()
生成一个表行列表,并做出反应。使用下面的代码时,I get
属性“forEach”在类型上不存在。有人知道如何解决这个问题吗?我不应该在persons上使用forEach(),因为它是PersonInterface数组吗

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>
  );
}
接口个人列表{
[索引:编号]:个人界面
}
接口人接口{
名称:string,
年龄:数目
}
const persons:个人列表=[
{
姓名:'约翰',
年龄:25岁,
}, {
名字:“吉尔”,
年龄:28,
}
];
导出默认函数表(){
返回(
{
persons.forEach(person=>{
返回(
{person.name}
);
})
}
);
}

您可以执行以下操作:-

interface Person {
  name: string, 
  age: number
}

const persons:Person[] = [
  {
    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>
  );
}
接口人{
名称:string,
年龄:数目
}
施工人员:人员[]=[
{
姓名:'约翰',
年龄:25岁,
}, {
名字:“吉尔”,
年龄:28,
}
];
导出默认函数表(){
返回(
{
persons.forEach(person=>{
返回(
{person.name}
);
})
}
);
}
您的
persons
变量不是有效的数组对象。您已将其类型设置为
PersonList
,它本身本质上就是一个类型。这就是为什么您得到的
属性“forEach”在type
上不存在的原因


还有一件事,
forEach
不会返回新数组。在此处使用
map
而不是
forEach

您可以执行以下操作:-

interface Person {
  name: string, 
  age: number
}

const persons:Person[] = [
  {
    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>
  );
}
接口人{
名称:string,
年龄:数目
}
施工人员:人员[]=[
{
姓名:'约翰',
年龄:25岁,
}, {
名字:“吉尔”,
年龄:28,
}
];
导出默认函数表(){
返回(
{
persons.forEach(person=>{
返回(
{person.name}
);
})
}
);
}
您的
persons
变量不是有效的数组对象。您已将其类型设置为
PersonList
,它本身本质上就是一个类型。这就是为什么您得到的
属性“forEach”在type
上不存在的原因


还有一件事,
forEach
不会返回新数组。在这里使用
map
而不是
forEach

以及您的键入问题,我猜您希望使用而不是

从“React”导入React
接口人接口{
名称:string
年龄:数目
}
const persons:PersonInterface[]=[
{
姓名:'约翰',
年龄:25岁,
},
{
名字:“吉尔”,
年龄:28,
},
]
常数表=()=>{
返回(
{persons.map(person=>(
{person.name}
))}
)
}
导出默认表

还有你的打字问题,我猜你想用而不是

从“React”导入React
接口人接口{
名称:string
年龄:数目
}
const persons:PersonInterface[]=[
{
姓名:'约翰',
年龄:25岁,
},
{
名字:“吉尔”,
年龄:28,
},
]
常数表=()=>{
返回(
{persons.map(person=>(
{person.name}
))}
)
}
导出默认表

forEach
是一个来自JS Class
数组的方法,如果你想在自定义类中使用它,它应该继承自
数组
,或者是Lakshya建议的一个(
persons:Person[]
)。

forEach
是一个来自JS Class
数组
的方法,如果你想在自定义类中使用它,它应该继承自
数组
,或者像Lakshya建议的那样,只继承一个(
persons:Person[]
)。欢迎来到SO。如果您能给出一个用于继承
数组的代码段,那么这可能是一个更好的答案。欢迎来到SO。如果您能给出一个用于继承
数组的代码片段,那么这可能是一个更好的答案。