我正在使用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 />