[英]Typescript Getting Child Props in Props
我是 Typescript 的新手。 假設我有一個名為TitleSubtitle
的組件,它返回一個Title
組件和一個Subtitle
組件。
Title
組件有 props:
interface TitleProps {
text: string;
}
Subtitle
組件有道具:
interface SubtitleProps {
text: string;
}
現在,在為TitleSubtitle
組件設置道具時,我可以這樣做:
interface TitleSubtitleProps {
titleText: string;
subtitleText: string;
}
或者,有沒有辦法做這樣的事情:
interface TitleSubtitleProps {
title: TitleProps;
subtitle: SubtitleProps;
}
這有意義嗎? 謝謝!
如果您希望分別指定title
和subtitle
為TitleProps["text"]
和SubtitleProps["text"]
類型,您可以這樣做 -
interface TitleSubtitleProps {
title: TitleProps["text"] // SubTitleProps.title: string
subtitle: SubtitleProps["text"] // SubtitleTitleProps.subtitle: string
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.