Skip to content

React PropTypes

New Courses Coming Soon

Join the waiting lists

How to use PropTypes to set the required type of a prop

Since JavaScript is a dynamically typed language, we don’t really have a way to enforce the type of a variable at compile time, and if we pass invalid types, they will fail at runtime or give weird results if the types are compatible but not what we expect.

Flow and TypeScript help a lot, but React has a way to directly help with props types, and even before running the code, our tools (editors, linters) can detect when we are passing the wrong values:

import PropTypes from 'prop-types'
import React from 'react'

class BlogPostExcerpt extends Component {
  render() {
    return (

BlogPostExcerpt.propTypes = {
  title: PropTypes.string,
  description: PropTypes.string

export default BlogPostExcerpt

Which types can we use

These are the fundamental types we can accept:

We can accept one of two types:


We can accept one of many values:

PropTypes.oneOf(['Test1', 'Test2']),

We can accept an instance of a class:


We can accept any React node:


or even any type at all:


Arrays have a special syntax that we can use to accept an array of a particular type:


Objects, we can compose an object properties by using

  color: PropTypes.string,
  fontSize: PropTypes.number

Requiring properties

Appending isRequired to any PropTypes option will cause React to return an error if that property is missing:

→ Get my React Beginner's Handbook
→ Read my full React Tutorial on The Valley of Code

Here is how can I help you: