我的React组件有一个名为prophichIsArray
的道具,它将是一个对象数组。这些对象将有一个id
,它是一个ID,而text
是一个字符串。你怎么能用TypeScript打这个?
type Props = {
propWhichIsArray: {
id,
text: string;
}[]
};
const Component: React.FC<[Props]> = ({ propWhichIsArray }) => {
//
我收到一个错误:
类型[Props]上不存在属性'prophicIsArray'
主要的问题是你正在做React。常设费用
interface Props {
propWhichIsArray: {
id: ID; // I assume ID is defined elsewhere
text: string;
}[]
}
const Component: React.FC<Props> = ({ propWhichIsArray }) => {
如果阵列中的此数据正在其他地方使用,您可能希望将其拉出到自己的接口:
interface Thingamajig {
id: ID;
text: string;
}
interface Props {
propWhichIsArray: Thingamajig[];
}