组件whichprop必须是特定React元素的数组

我正在尝试编写一个组件,它接受另一个组件的实例数组作为道具。该组件 is MyComponent,它接受一组 的实例Title作为其参数之一。但是,我无法让 TypeScript 进行类型检查:

import * as React from "react";


type TitleProps = {
    name: string;
};

function Title(props: TitleProps) {
    return null;
}

type MyComponentProps = {
    titles: Array<React.ReactElement<TitleProps>>;
}

function MyComponent({ titles }: MyComponentProps) {
    return <div>{titles}</div>;
}

function OtherComponent(props: {}) { return null }

const shouldError = <MyComponent titles={[ <div/>, <OtherComponent/> ]} />;
const shouldNotError = <MyComponent titles={[ <Title name="hi"/>, <Title name="hi2"/>, ]} />;

如您所见,我可以将任何我想要的东西传递给titlesprop,而不仅仅是<Title/>.

打字稿游乐场网址

以上是组件whichprop必须是特定React元素的数组的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>