Reactjs React HOC with TypeScript:可以使用不同的约束子类型进行实例化
我要做的是编写一个HOC,它用一个强制属性Reactjs React HOC with TypeScript:可以使用不同的约束子类型进行实例化,reactjs,typescript,typescript-generics,Reactjs,Typescript,Typescript Generics,我要做的是编写一个HOC,它用一个强制属性值包装一个组件,但不传递它自己的属性名称 import React,{ComponentType}来自'React'; 接口IPassThroughProps{ 值:字符串; } 接口IHocPropsType{ 名称:字符串; } 导出功能hoc( 组件:组件类型 ):组件类型{ const hoc=({name,…props}:IHocPropsType&PropsType):JSX.Element=>( ); 返回hoc; } 问题是生成了键入错
值
包装一个组件,但不传递它自己的属性名称
import React,{ComponentType}来自'React';
接口IPassThroughProps{
值:字符串;
}
接口IHocPropsType{
名称:字符串;
}
导出功能hoc(
组件:组件类型
):组件类型{
const hoc=({name,…props}:IHocPropsType&PropsType):JSX.Element=>(
);
返回hoc;
}
问题是生成了键入错误:
类型Pick
不能分配给类型内部属性和属性类型&{children?:ReactNode;}
类型Pick
不能分配给类型PropsType
Pick
可分配给类型为PropsType
的约束,但是PropsType
可以使用约束的不同子类型IPassThroughProps
实例化
对如何修复它有什么建议吗
我想我需要用这一点做点什么:
IHocPropsType&PropsType
但是不太清楚如何表达它。解决它的正确方法是使用
省略util,同时键入..props
:
import React, { ComponentType } from 'react';
interface IPassThroughProps {
value: string;
}
interface IHocPropsType {
name: string;
}
export function hoc<PropsType extends IPassThroughProps>(
Component: ComponentType<PropsType>
): ComponentType<IHocPropsType & Omit<PropsType, 'name'>> => {
const hoc = (
{ name, ...props }: IHocPropsType & Omit<PropsType, 'name'>
): JSX.Element => (
<Component {...(props as PropsType)} />
);
return hoc;
}
import React,{ComponentType}来自'React';
接口IPassThroughProps{
值:字符串;
}
接口IHocPropsType{
名称:字符串;
}
导出功能hoc(
组件:组件类型
):ComponentType=>{
常量hoc=(
{name,…props}:IHocPropsType&Omit
):JSX.Element=>(
);
返回hoc;
}
我认为这意味着我们不能从泛型中排除字段