【发布时间】:2017-11-12 01:00:23
【问题描述】:
鉴于以下组件,我收到一个关于缺少 isUpdating 道具的错误。我该如何解决?
错误:(87, 27) TS2322:Type '{ name: "Fred"; }' 不可分配给类型 'IntrinsicAttributes & IntrinsicClassAttributes> & Reado...'。 键入'{名称:“弗雷德”; }' 不可分配给类型“只读”。 类型 '{ name: "Fred"; 中缺少属性 'isUpdating'; }'。
interface ContaineeProps {
name: string;
isUpdating: string;
}
const Containee = (props: ContaineeProps) => <span>{props.name}</span>;
interface MapStateToProps {
isUpdating: boolean;
}
const mapStateToProps = state => ({ isUpdating: state.years.yearsUpdating });
const ConnectedContainee = connect<MapStateToProps, null, ContaineeProps>(mapStateToProps)(Containee);
interface Container {
name: string;
}
class Container extends React.Component<Container, {}> {
render() {
return (
<ConnectedContainee name="Fred" />
)
}
}
编辑:这个问题更多的是关于 redux/react/typescript 应用程序中的最佳实践,而不是关于这个错误的原因。我是否总是必须在两个地方(组件的 props 接口和 mapStateToProps 函数的接口)指定所需的 props 类型信息?
【问题讨论】:
-
什么是 VehiclesFormProps?
-
@OliverCharlesworth 好废话,我以为我从我的实际组件中复制/粘贴/编辑一个简化的示例做了如此仔细的工作。原来我真的很烂! (修正了我的例子)
标签: reactjs typescript redux react-redux