reactjs - 可分配给 'P' 类型的约束,但 'P' 可以用约束 'TextInputProps' 的不同子类型实例化

标签 reactjs typescript

这是 HOC,我有以下错误:

Type 'Pick, "children" | Exclude> & { ...; }' is not assignable to type 'IntrinsicAttributes & P & { children?: ReactNode; }'

Type 'Pick, "children" | Exclude> & { ...; }' is not assignable to type 'P'.

'Pick, "children" | Exclude> & { ...; }' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'TextInputProps'.



怎么修?
import { View, ViewStyle, StyleProp, NativeSyntheticEvent, TextStyle, TextInputProps } from 'react-native';

type Props = {
  styleWrapper?: StyleProp<ViewStyle>;
  style?: StyleProp<TextStyle>;
  title?: string;
  value?: string;
  onFocus?: (e: NativeSyntheticEvent<any>) => void;
  onBlur?: (e: NativeSyntheticEvent<any>) => void;
  onChange?: (text: string) => void;
  onReset?: () => void;
  withReset?: boolean;
};

export const withInputWrapper = <P extends TextInputProps = TextInputProps>(
  InputComponent: React.ComponentType<P>
): React.FC<P & Props> => {
  return ({ styleWrapper, style, title, value, onFocus, onBlur, onChange, onReset, withReset = true, ...props }) => {
    const onFocusHandler = ...
    const onBlurHandler = ...

    const onChangeHandler = ...

    return (
      <View style={styleWrapper}>
        <View style={s.inputWrapper}>
          <InputComponent // <-- here
            {...props}
            onChange={onChangeHandler}
            value={value}
            style={style}
            onBlur={onBlurHandler}
            onFocus={onFocusHandler}
          />
        </View>
      </View>
    );
  };
};

最佳答案

这是当您达到 TS 可以对具有条件和映射类型的泛型类型参数进行推理的限制时的情况之一。虽然 props可能看起来很明显 P ,这对编译器来说并不明显。编译器将使用 Omit输入 props它使用映射类型和条件类型从给定类型中删除一些键。所以 props 将被输入为 Omit<P & Props, keyof Props> .这可能看起来很明显 P ,但 TS 不能遵循条件类型,只要它们仍有未解析的类型参数。这意味着,就 TS 而言 Omit<P & Props, keyof Props>P是不同的类型。

这里唯一的解决方案是使用类型断言:

export const withInputWrapper = <P extends TextInputProps = TextInputProps>(
  InputComponent: React.ComponentType<P>
): React.FC<P & Props> => {
  return ({ styleWrapper, style, title, value, onFocus, onBlur, onChange, onReset, withReset = true, ...props }) => {

    const onFocusHandler = () => {}
    const onBlurHandler = () => {}

      const onChangeHandler = () => {}

    return (
      <View style={styleWrapper}>
        <View>
          <InputComponent // <-- here
            {...props as P}
            onChange={onChangeHandler}
            value={value}
            style={style}
            onBlur={onBlurHandler}
            onFocus={onFocusHandler}
          />
        </View>
      </View>
    );
  };
};

Playground Link

关于reactjs - 可分配给 'P' 类型的约束,但 'P' 可以用约束 'TextInputProps' 的不同子类型实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60735375/

相关文章:

javascript - 将 "class" "instances"传递给 ReactJS 组件以代替 `props` 文字

reactjs - 在React-Router中隐藏Not Found页面上的公共(public)组件

javascript - 获取请求给出错误

typescript - VSCode 仅在导入符号时呈现 JSDoc 链接到另一个文件

javascript - Typescript 类构造函数生成的代码顺序

ReactJS - Babel 版本问题

javascript - 根据 Material-UI 组件的 props 动态创建第 n 个子选择器

javascript - 如何在单独的选择组件中键入 forwardRef?

typescript - 如何修复此 typescript 文件 "is not a module"错误?

javascript - 通过代码更改 Angular 垫选择占位符颜色