HurricaneJames / react-immutable-proptypes

PropType validators that work with Immutable.js.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

way to extend PropType?

rkoberg opened this issue · comments

Say I have a base form PropType:

export const formPropType = ImmutablePropTypes.recordOf({
  name: PropTypes.string.isRequired,
  disabled: PropTypes.boolean.isRequired,
  error: PropTypes.object,
  fields: ImmutablePropTypes.record,
});

export const Form = Record({
  name: 'login',
  disabled: false,
  error: null,
  fields: null
});

I would like to 'extend' or somehow reuse the formPropType. Is it possible? Maybe something like:

// .set (and setIn) would return a new PropType similiar to immutable
export const loginFormPropType = formPropType.set({
  fields: loginFieldsPropType.isRequired,
});

export const LoginForm = Form({
  name: 'login',
  fields: LoginFields(),
});

Interesting idea, but I think ultimately misplaced. In my experience PropTypes serve two purposes. First, they provide dev console output when something is wrong. Second, and I have found more importantly, they let the developer know at a glance the props used by a component. Adding inheritance would mean spreading out those props to different files and/or having props that the component does not actually use. That said, I'm willing to listen to reason why I'm wrong.

No worries. I can use plain objects with Object.assign({}, baseForm, {...someFormProps}).

`export const ComponentAProps = {value: PropTypes.string};
ComponentA.propTypes = ComponentAProps;

export const ComponentBProps = { ....ComponentAProps, onChange: PropTypes.func };
ComponentB.propTypes = ComponentBProps`

If I would ask this question, I would consider TypeScript at this point.