简体   繁体   English

Typescript 在道具中获取儿童道具

[英]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 -如果您希望分别指定titlesubtitleTitleProps["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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM