React: oneOfType usage

Viewed 29

I see that another developer is using oneOfType like this:

size: PropTypes.oneOfType([PropTypes.object]).isRequired,

It doesn't seem right to me. The way I understand oneOfType is that the prop should be one of a specified set of types like the example bellow:

size: PropTypes.oneOfType([
PropTypes.object,
PropTypes.number
]).isRequired,

However,

size: PropTypes.oneOfType([PropTypes.object]).isRequired,

doesn't throw any linting errors and everything seems to be working fine.

Can I leave it as it is?

1 Answers

oneOfType is to consider one of some types will be valid

size: PropTypes.oneOfType([
PropTypes.object,
PropTypes.number
]).isRequired,

The above oneOfType means size can be object or number. And isRequired is to make sure you pass size value instead of leaving it empty.


size: PropTypes.oneOfType([PropTypes.object]).isRequired,

With the above condition, size can be object only (no other types). And it's totally fine.

You also can consider changing the above condition to the below one which has the same goal.

size: PropTypes.object.isRequired,
Related