如何限制某些 children 类型的 React.Fragment?

How to restrict to React.Fragment of certain children types?

我有一个用流输入的组件,它只允许某些类型的元素,一个非常有限的列表。

现在我需要扩展它以接受 Fragment,这样我们就可以进行一些渲染逻辑和分组 children。但是,我只想获取包含有效组件的特定子集的片段,就像普通类型一样。

这就是我现在拥有的:

type ValidChilds =
  | item
  | header
  | separator

type PropsA = {
  className?: string,
  children: ChildrenArray<Element<ValidChilds>>
}

正如我所说,这对限制 children 非常有效。 所以我现在想要的是能够接受这个:

<>
<item>
<header>
</>

但不是这个:

<>
<other>
</>

我试过了,但没用:

type ValidChilds =
  | item
  | header
  | separator
  | AbstractComponent<{ children: ValidChilds }, typeof React.Fragment>

type PropsA = {
  children: ChildrenArray<Element<ValidChilds>>
}

看完 React.Fragment definitions, I gave it a try, but it looks like it's not possible currently, as we can't override the core Fragment statement definitions.

打开一个问题可能是最糟糕的,因为它绝对是一个有用的案例。