用React Redux`connect`包装组件不会丢失prop类型的requiremnets

时间:2019-08-09 00:00:17

标签: typescript redux react-redux

我正在使用react-redux的{​​{1}}函数包装一个组件,但是该组件上的返回类型始终为connect,并且在编译时不会检查所需的prop。 / p>

代码如下:

any

即使需要export interface OwnProps { plans: Plan[]; api: ApiService; onChange?(plan: Plan): void; } export interface StateProps { currentPlan: Plan; } export type PlanUpdateProps = OwnProps & StateProps; export interface PlanUpdateState { isYearly: boolean; } class _PlanUpdate extends React.Component <PlanUpdateProps, PlanUpdateState> { constructor(props: PlanUpdateProps) { super(props); this.state = { isYearly: props.currentPlan.interval === 'year', }; } public render() { const { plans, api, onChange } = this.props; const { isYearly } = this.state; const interval: Plan['interval'] = isYearly ? 'year' : 'month'; const filteredPlans = plans.filter(plan => plan.interval === interval).sort((planA, planB) => planA.amount < planB.amount ? -1 : 1 ); return ( // some JSX ); } } function mapStateToProps(state: StoreState): StateProps { return { currentPlan: state.entities.currentPlan!, }; } export const PlanUpdate = connect<StateProps, null, OwnProps, StoreState>(mapStateToProps)(_PlanUpdate); PlanUpdate道具,将api包含在另一个组件中也不会导致编译器错误:

plans

相关包装信息:

    <PlanUpdate />

0 个答案:

没有答案