请问如何对数组类型type提出一条元素 & 其他的type,进而做一个props?

我现在有如下的props

export type TabsCompProps = {
  label: string,
  children: React.ReactNode,
  key: string,
  breadcrumbs: string[] 
}[]


export type DetailProps = {
  content: string 
}

我想要做一个props如下:

type TabsWithDetailProps = {...}[]

TabsWithDetailProps是一个数组,数组里面的元素由TabsCompProps数组提出的一条信息和DetailProps 组成。请问应该如何做呢?

阅读 2.1k
2 个回答
type TabsCompProps = {
  label: string;
  children: React.ReactNode;
  key: string;
  breadcrumbs: string[];
}[];

type DetailProps = {
  content: string;
};

type TabsWithDetailProps = (TabsCompProps[number] & DetailProps)[];

// 示例
const tabsWithDetail: TabsWithDetailProps = [
  {
    label: "Tab 1",
    children: <div>Content 1</div>,
    key: "1",
    breadcrumbs: ["Home", "Tab 1"],
    content: "Detail Content 1",
  },
  {
    label: "Tab 2",
    children: <div>Content 2</div>,
    key: "2",
    breadcrumbs: ["Home", "Tab 2"],
    content: "Detail Content 2",
  },
];

ts-playground

export type TabsCompProps = {
  label: string,
  children: React.ReactNode,
  key: string,
  breadcrumbs: string[] 
}[]


export type DetailProps = {
  content: string 
}

type TabsWithDetailProps = (TabsCompProps[number]&DetailProps)[]
let a:TabsWithDetailProps=[{
  content:'1',
  key:'1',
  breadcrumbs:['1'],
  label:'1',
}]

export type ExpandRecursively<T> = T extends object
  ? T extends infer O
    ? { [K in keyof O]: ExpandRecursively<O[K]> }
    : never
  : T

/** Expand a type */
export type a= ExpandRecursively<TabsWithDetailProps> 
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏