prop-types icon indicating copy to clipboard operation
prop-types copied to clipboard

Add a PropType for an exact shape plus "...rest" properties

Open effulgentsia opened this issue 2 years ago • 1 comments

Suppose I want something like this:

PropTypes.exact({
  x: PropTypes.boolean,
  y: PropTypes.string.isRequired,
  "...rest": PropTypes.objectOf(PropTypes.string)
})

The above won't work because I don't want to apply that last validator to a property named ...rest, I want to apply it to the rest object that you would get when you destructure the remaining properties.

Is this possible to do with existing prop types, or would it require a new prop type, such as:

PropTypes.exactPlusRest({
  x: PropTypes.boolean,
  y: PropTypes.string.isRequired
}, PropTypes.string)

effulgentsia avatar Nov 16 '23 16:11 effulgentsia

If not for x, you could combine y and objectOf and it’d work fine.

I suspect you can use the combinators in npmjs.com/airbnb-prop-types to do what you want, but it’d be a bit cumbersome. I’ll have to think on this one.

ljharb avatar Dec 01 '23 15:12 ljharb