打字稿反应成分抱怨缺少注入的属性

时间:2020-05-21 18:39:51

标签: reactjs typescript

我正在编写打字稿组件,并且在将值注入道具时遇到错误。我希望注入的信息包含在prop类型中,但是当我尝试渲染组件时,我会遇到智能错误,说我没有传递所有必要的prop。一种快速的解决方法是将prop类型标记为自身的Partial,但这只是使渲染线起作用而不能准确反映prop类型的一种技巧(然后,我需要向永远存在的属性)。有什么方法可以以不需要道具的方式来键入组件,而道具本身仍然存在。

export type WrappedComponentProps = {
  user: User,
  id: string
};

@inject('user')
class WrappedComponent extends Component<WrappedComponentProps> {

  get id(){
    return this.props.id;
  }

  render(){
    console.log(this.props.user);
    return (
      <h1>hi there {this.props.user.name} id:{this.id}</h1>
    );
  }

}


const WrapperComponent = ({ id }: {id: string}) => {
  return (
    <div>
      wrapper with id: {id}
      <WrappedComponent id />// complains that there is no user prop
    </div>
  );
};

export default WrapperComponent;

0 个答案:

没有答案