Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to extend a PropTypes shape #314

Open
luizpinheiro opened this issue Apr 26, 2020 · 1 comment
Open

Ability to extend a PropTypes shape #314

luizpinheiro opened this issue Apr 26, 2020 · 1 comment
Labels
new validator request Requests for a new kind of validator. question

Comments

@luizpinheiro
Copy link

luizpinheiro commented Apr 26, 2020

I've seen some projects that define prop-types in separate files in order to reuse their definitions. The problem is that we can't extend prop-types definitions. Let's say we have a set of prop-types called Person.type.js:

export default PropTypes.shape({
    id: PropTypes.number,
    name: PropTypes.name,
})

The problem is that we can't do something like:

import PersonType from './Person.type.js'

export default PropTypes.shape({
    ...PersonType,
    someAdditionalProp: PropTypes.any
})

Also, exporting a plain object from 'Person.type.js' is not an option since we may want the ability to use those properties directly, and for the matter of consistency, we may not want to use it like PropTypes.shape(PersonType) (that would be a possible workaround) whenever we use the PersonType directly as a prop.

So would be great to have a method like PropTypes.extend(shape, additionalFields) so we could be able to write something like:

import PersonType from './Person.type.js'

export default PropTypes.extend(PersonType, {
    someAdditionalProp: PropTypes.any
})
@ljharb
Copy link
Collaborator

ljharb commented Apr 27, 2020

If you use and from https://npmjs.com/airbnb-prop-types, you can do and([shape1, shape2]) and combine them.

@ljharb ljharb added new validator request Requests for a new kind of validator. question labels Apr 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new validator request Requests for a new kind of validator. question
Projects
None yet
Development

No branches or pull requests

2 participants