chengaofeng
发布于 2024-08-03 / 22 阅读
0
0

Typescript如何限制两个对象的key相同

export interface ListCardProps {
  height?: number;
  moreText?: string;
  tabList: Record<string, React.ReactNode>;
  contentList?: Record<
    string,
    {
      url: string;
      title: string;
      time: string;
    }[]
  >;
}

如上面代码,如何保证tabList的key,在contentList中一一对应?

其实不要为难自己,两个合成一个就可以了。一个tab包含标签和内容,更好描述

export interface ListCardProps {
  height?: number;
  moreText?: string;
  tabs: Record<
    string,
    {
      tab: React.ReactNode;
      content: {
        url: string;
        title: string;
        time: string;
      }[];
    }
  >;
}


评论