Reactjs 扩展接受的道具类型
我有一个组件,它的根节点可以被passed prop的值覆盖Reactjs 扩展接受的道具类型,reactjs,typescript,Reactjs,Typescript,我有一个组件,它的根节点可以被passed prop的值覆盖 界面道具{ propA?:字符串 as?:React.ElementType } 常量可重写组件=(道具:道具)=>{ const{as:Tag='div',…otherProps}=props 返回 } 以及另一个将用作的组件 interface-SomeComponentProps{ propB:string//{ 返回{props.children} } 所以,我想要达到的结果是这样的 我希望OverridableCom
界面道具{
propA?:字符串
as?:React.ElementType
}
常量可重写组件=(道具:道具)=>{
const{as:Tag='div',…otherProps}=props
返回
}
以及另一个将用作的组件
interface-SomeComponentProps{
propB:string//{
返回{props.children}
}
所以,我想要达到的结果是这样的
我希望OverridableComponent
合并SomeComponent
中的道具
是否可以将所需的React.ElementType
明确地传递为?这样做,您就不依赖于编译器推理,并且可以适当地缩小类型
组成部分:
const AppWithComponent = () => (
<OverridableComponent<typeof SomeComponent>
propA="pa"
propB="pb"
// className="ajsf" ; error
// from='string' ; error
as={SomeComponent}
/>
);
const-AppWithComponent=()=>(
);
固有元素:
const AppWithIntrinsicElementExplicit = () => (
<OverridableComponent<"div">
propA="pa"
// propB="pb" ; error
className="yehaa"
// from='string' ; error
as="div" // you also can drop this attribute
/>
);
const-AppWithIntrinsicElementExplicit=()=>(
);
好吧,我想避免在jsx中使用类型,但这是一个选项,谢谢