[英]Typescript Getting Child Props in Props
I am new to Typescript.我是 Typescript 的新手。 Let's say I have a component called TitleSubtitle
which returns both a Title
component and a Subtitle
component.假设我有一个名为TitleSubtitle
的组件,它返回一个Title
组件和一个Subtitle
组件。
The Title
component has props: Title
组件有 props:
interface TitleProps {
text: string;
}
The Subtitle
component has props: Subtitle
组件有道具:
interface SubtitleProps {
text: string;
}
Now, when setting my props for the TitleSubtitle
component, I could do this :现在,在为TitleSubtitle
组件设置道具时,我可以这样做:
interface TitleSubtitleProps {
titleText: string;
subtitleText: string;
}
Or, is there some way to do something like this :或者,有没有办法做这样的事情:
interface TitleSubtitleProps {
title: TitleProps;
subtitle: SubtitleProps;
}
Does this make sense?这有意义吗? Thanks!谢谢!
If you're looking to specify title
and subtitle
as specifically of type TitleProps["text"]
and SubtitleProps["text"]
respectively, you can do that -如果您希望分别指定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.