Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 为什么typescript不能确保在React中添加附加属性的通用高阶组件中的类型安全?_Reactjs_Typescript_Generics_Higher Order Components - Fatal编程技术网

Reactjs 为什么typescript不能确保在React中添加附加属性的通用高阶组件中的类型安全?

Reactjs 为什么typescript不能确保在React中添加附加属性的通用高阶组件中的类型安全?,reactjs,typescript,generics,higher-order-components,Reactjs,Typescript,Generics,Higher Order Components,我正在尝试创建一个通用的高阶组件,并且没有检查类型安全性。当以非通用方式执行相同操作时,将按预期检查类型安全性 interface IBaseProps { baseValue?: string; } const Base = (props: IBaseProps) => { return <span>{"value: " + props.baseValue}</span>; }; interface IExtraProps {

我正在尝试创建一个通用的高阶组件,并且没有检查类型安全性。当以非通用方式执行相同操作时,将按预期检查类型安全性

interface IBaseProps {
  baseValue?: string;
}

const Base = (props: IBaseProps) => {
  return <span>{"value: " + props.baseValue}</span>;
};

interface IExtraProps {
  extraValue?: string;
}

const foo = <T extends IBaseProps>(
  Element: React.ComponentType<T>
): React.ComponentType<T & IExtraProps> => (props: T & IExtraProps) => {
  const baseValue = `${props.baseValue} extra value: ${props.extraValue}`;
  return <Element {...props} baseValue={baseValue} />; // example
};

const bar = <T extends IBaseProps>(
  Element: React.ComponentType<T>
): React.ComponentType<T & IExtraProps> => (props: T & IExtraProps) => {
  return <Element {...props} baseValue={42} />; // NO error, why?
};

const baz = <T extends IBaseProps>(
  Element: React.ComponentType<T>
): React.ComponentType<T & IExtraProps> => (props: T & IExtraProps) => {
  return <Element {...props} unknownProp={42} />; // NO error, why?
};

const bag = (props:IBaseProps) => {
  const baseValue = props.baseValue + ' augmented'
  return <Base {...props} baseValue={42} />; // ok, error is detected
}
接口IBaseProps{
baseValue?:字符串;
}
常量基=(道具:IBaseProps)=>{
返回{“value:+props.baseValue};
};
接口IExtraProps{
extraValue?:字符串;
}
常数foo=(
元素:React.ComponentType
):React.ComponentType=>(道具:T&iextrapps)=>{
const baseValue=`${props.baseValue}额外值:${props.extraValue}`;
return;//示例
};
常数条=(
元素:React.ComponentType
):React.ComponentType=>(道具:T&iextrapps)=>{
return;//没有错误,为什么?
};
常数baz=(
元素:React.ComponentType
):React.ComponentType=>(道具:T&iextrapps)=>{
return;//没有错误,为什么?
};
const bag=(道具:IBaseProps)=>{
常量baseValue=props.baseValue+“增强”
return;//确定,检测到错误
}
可以找到演示


如何使Typescript正确地检查类型?

Typescript一切正常。基本组件在上面声明,其类型为IBaseProps。这就是它显示错误的原因

酒吧有

React.ComponentType<T & IExtraProps> 
React.ComponentType
它基本上意味着T可以是任何类型。所以它没有显示任何错误

其他也有T&IExtrapps类型。
通过将鼠标悬停到其中任何一个(酒吧、酒吧、美食、包)进行检查。此代码中的所有内容都正常工作。

这是一个很好的问题!很遗憾,我对你的演示感到困惑,无法回答。我没有遇到过这种情况,因为通常在使用HOC时,我会删除道具需求(通过生成它们),而不是添加额外的必需道具。