类型“元素 \[\]”缺少类型“元素”的以下属性:类型、道具、键

新手上路,请多包涵

我有带有 Typescript 和 React 环境的标准箭头映射 ES7 函数:

  const getItemList: Function = (groups: any[]): JSX.Element =>
  group.map((item: any, i: number) => {
    const itemCardElemProps = { handleEvents: () => {}, ...item}
    return <Item key={`${item.id}_${i}`} {...itemCardElemProps} />
  })

并得到错误:

 TS2739: Type 'Element[]' is missing the following properties from type 'Element': type, props, key

版本:打字稿3.5.3

原文由 Roman 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 458
1 个回答

您也可以始终将单个 JSX.Element 作为片段发回:

 interface IOptions {
   options: string[]
}

const CardArray: React.FC<IOptions> = ({ options }) => {
   return <>{options.map(opt => opt)}</>
}

这样您就可以匹配返回的类型,并且不会影响您的标记。

原文由 andrewmart.in 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进