我正在创建一个包装另一个名为 Button
的组件的组件 . Button
在库中定义,不导出 Button
属性的类型 .
要正确键入我的组件,最好复制 Button
属性的类型,向该类型添加一些属性(使用 typeof
, $Diff
或类似)并将其用作我的组件 StyledButton
的类型 .
就像是:
type AddedPropsT = {
isStyled: boolean,
}
function StyledButton(props: $GenericParam(typeof Button) & AddedPropsType) {
return (<Button ...props className={isStyled ? "StyleClass" : ""} />)
}
当然,在Flow中不存在类型util $GenericParam
. Button
的类型是React.ComponentType<Props>(问题是如何从那里获得 Props
类型)并且在幕后它是纯函数无状态组件 .
1 回答
您可以使用React.ElementConfig<typeof Component>实用程序类型来获取Button的道具类型: