HurricaneJames / react-immutable-proptypes

PropType validators that work with Immutable.js.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validate a map's key type and value type

qimingweng opened this issue · comments

Would be cool to have!

For example

map: ImmutablePropTypes.mapKeyValue(PropTypes.string, PropTypes.number)

I have never used anything but strings or numbers as keys, especially for data I'm passing as a prop. I am curious what your use case is for needing to map the type of the key?

I should have some time next week to work on this. Right now I'm working on a writeup for another project. If you already have an implementation, I'm open to a PR.

I still use primarily strings as keys, but there are three-ish scenarios (all somewhat vague, I'll admit) that I've found a need for something like this

  1. I use PropTypes also as a form of documentation on what the data the component uses should look like. So it's useful to be more declarative about what the keys are, for example, PropTypeAccountID = PropTypes.string.
  2. If IDs (or keys) have to adhere to a certain string format (ie, 6 or more characters), I can use PropTypeAccountID = PropTypes.customProp(...).
  3. I've made the mistake before of generating optimistic object ids as numbers before the server generates strings, and it's possible to expect Map keys as strings but get numbers instead, so even just to distinguish between strings and numbers can be useful.

But I do love this little library 😄. I haven't had time to implement yet but if I do before you I'll submit a PR.

Actually implemented in PR: #30