Javascript 强制执行props.children在TypeScript中的类型

Javascript 强制执行props.children在TypeScript中的类型,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我正在尝试创建一个React组件,它只能接收特定类型的子级。更具体地说,接收不同组件的子级 这样做的目的是在组成组件的子组件时具有智能感知 例如,这应该很好: <PlantParent name="Sammy" age={26}> <Plant name="Macadamia" species="cactus" ageInMonths={6} /> <Plant name="Chocolate Cheesecake" species="succulent"

我正在尝试创建一个React组件,它只能接收特定类型的子级。更具体地说,接收不同组件的子级

这样做的目的是在组成组件的子组件时具有智能感知

例如,这应该很好:

<PlantParent name="Sammy" age={26}>
  <Plant name="Macadamia" species="cactus" ageInMonths={6} />
  <Plant name="Chocolate Cheesecake" species="succulent" ageInMonths={24} />
</PlantParent>
但这样做会使上述两种情况都失败,而不仅仅是第二种情况,错误如下:

Type '{ children: Element[]; name: string; age: number; }' is not assignable to type 'PlantParentProps'.
    Types of property 'children' are incompatible.
        Type 'Element[]' is not assignable to type 'FunctionComponent<PlantItem>'.
类型“{children:Element[];name:string;age:number;}”不能分配给类型“PlantParentProps”。
属性“children”的类型不兼容。
类型“Element[]”不可分配给类型“FunctionComponent”。
这是有道理的,因为我不确定
的孩子属于
类型,但我只是不知道接下来要采取什么步骤,或者如何确保/如果孩子属于某种类型

我在这里举了一个例子,上面的代码以及Plant和Plant Parent的实现:


谢谢

找到此项可能有助于尝试
子项:排除
但不起作用
type PlantParentProps = {
  name: string;
  age: number;
  children: React.FC<PlantItem>;
};
Type '{ children: Element[]; name: string; age: number; }' is not assignable to type 'PlantParentProps'.
    Types of property 'children' are incompatible.
        Type 'Element[]' is not assignable to type 'FunctionComponent<PlantItem>'.