Home > Blockchain >  making enums part of prop-types validation
making enums part of prop-types validation

Time:11-18

In my component, I want a props to be an enum as follows:

enum choices {
wide,
medium,
small
}

interface props {
    size: choices
}


function SizeSelector({size}: props){
//...do stuff
}

then I want to do prop-types on it:

SizeSelector.propTypes = {
  size: PropTypes.oneOf(choices) //doesn't work
}

How do I make it part of the PropTypes validation?

CodePudding user response:

Try this

  SizeSelector.propTypes = {
    size: PropTypes.oneOf(['wide', 'medium', 'small'])
  }
  • Related