提问者:小点点

React组件的TypeScript类型,其中prop是对象的数组?


我的React组件有一个名为prophichIsArray的道具,它将是一个对象数组。这些对象将有一个id,它是一个ID,而text是一个字符串。你怎么能用TypeScript打这个?

type Props = {
  propWhichIsArray: {
    id,
    text: string;  
  }[]
};

const Component: React.FC<[Props]> = ({ propWhichIsArray }) => {
  //

我收到一个错误:

类型[Props]上不存在属性'prophicIsArray'


共1个答案

匿名用户

主要的问题是你正在做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[];
}