Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

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
Contributor

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new validator request Requests for a new kind of validator. question
Projects
None yet
Development

No branches or pull requests

2 participants