【发布时间】:2021-10-30 23:04:03
【问题描述】:
我正在尝试在 React 中为数组创建一个类型,但出现以下错误。我不确定我在这里缺少什么。
谁能帮我找出我缺少的东西?
谢谢
interface RowsData {
[key: string]: string | number | JSX.Element;
}
const rows: RowsData = [
{ id: 1, name: 'A', action: <div style={{ color: 'blue' }}> Hello </div> },
{ id: 2, name: 'B', action: <div style={{ color: 'blue' }}> World </div> },
{ id: 3, name: 'C', action: <div style={{ color: 'blue' }}> Foo </div> }
];
错误
Type '{ id: number; name: string; action: JSX.Element; }' is not assignable to type 'string | number | Element'.
Object literal may only specify known properties, and 'id' does not exist in type 'Element'.ts(2322)
【问题讨论】:
-
您的意思是
const rows: RowsData[] = [...?即声明行的类型为RowData[], notRowsData.
标签: reactjs typescript typescript-typings