Skip to the solution
EasyFrontend6 min read

What is PropTypes and when would you use it?

1.1k views

The problem

Question presented to candidate: "What is PropTypes, and when would you reach for it today?"

What a strong answer should cover:

  • PropTypes was React's runtime prop type-checking mechanism: attach a propTypes object and React warns in the console when a prop has the wrong type.
  • It ran in development only and was always a warning, never an error β€” it never blocked a render.
  • The history matters: React.PropTypes moved out to the standalone prop-types package in React 15.5.
  • The key fact: React 19 removed propType checking entirely. A propTypes object on a component is now silently ignored β€” no validation, no warning.
  • defaultProps was likewise removed for function components in React 19, in favour of ES6 default parameters. Class components keep it.
  • So the honest answer to "when would you use it" is: not in new code. Use TypeScript, which catches the same errors at compile time and across the whole call site.
  • The remaining niche: validating data crossing a runtime boundary β€” API responses, plugin inputs β€” which is a job for Zod or Valibot, not PropTypes.

Clarifying questions expected:

  • "Which React version is this codebase on?" β€” the answer changes completely at 19.
  • "Is the project on TypeScript already?"

Code / implementation expected: Optional. Showing the TypeScript and default-parameter replacement for a propTypes + defaultProps pair is the useful version.

  • prop-types
  • type checking
  • validation
  • development

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Try it in the playground

Open any example in the playground to run and change it.

ReactVerifying propTypes is inert on React 19, and the modern replacement
import { useState } from "react";

// ── The legacy pattern ─────────────────────────────────────────────────────
// A validator designed to scream if it is ever called. On React 19 it is not.
let validatorCalls = 0;
function LegacyGreeting({ name, age }) {
  return <p>{name} is {age}</p>;
}
LegacyGreeting.propTypes = {
  name: () => { validatorCalls += 1; return new Error("name is wrong!"); },
  age:  () => { validatorCalls += 1; return new Error("age is wrong!"); },
};
// Also removed for function components in React 19 β€” silently stops applying.
LegacyGreeting.defaultProps = { age: 99 };

// ── The modern replacement ─────────────────────────────────────────────────
// Default values are plain ES6 default parameters, readable right where the
// prop is destructured. In a .tsx file the types would live here too:
//   function ModernGreeting({ name, age = 99 }: { name: string; age?: number })
function ModernGreeting({ name, age = 99 }) {
  return <p>{name} is {age}</p>;
}

export default function App() {
  const [checked, setChecked] = useState(false);

  return (
    <div style={{ padding: 24, fontFamily: "system-ui", lineHeight: 1.6 }}>
      <h4>Legacy β€” propTypes attached, deliberately wrong props passed</h4>
      {/* name should be a string, age a number: both are wrong on purpose */}
      <LegacyGreeting name={12345} age="not a number" />
      {/* A div, not a p: LegacyGreeting renders a <p> of its own, and a p
          inside a p is invalid HTML the browser silently repairs. */}
      <div>defaultProps age fallback: <LegacyGreeting name="Ada" /></div>

      <h4>Modern β€” ES6 default parameter</h4>
      <ModernGreeting name="Ada" />

      <button onClick={() => setChecked(true)}>Check whether validators ran</button>
      {checked && (
        <p style={{ marginTop: 12, padding: 12, background: "#fff3cd", borderRadius: 6 }}>
          propTypes validators called: <strong>{validatorCalls}</strong>
          {validatorCalls === 0
            ? " β€” React 19 ignores propTypes entirely. No warning, no validation."
            : " β€” this React version still honours propTypes."}
          <br />
          Notice too that the legacy defaultProps age fallback did not apply,
          while the ES6 default parameter did.
        </p>
      )}
    </div>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 3 of 119 in React.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All React.js questions
Up nextEasy

createContext vs use(Context) β€” new context consumption in React 19

Continue