Skip to content

Inferring types from React PropTypes

Posted on:July 11, 2024

These are super simple examples of how to infer Typescript or JSDoc types from React PropTypes. They do not take into account MyComponent.defaultProps.

TypeScript example

import PropTypes from "prop-types";

function MyComponent (({ id, name }): PropTypes.InferProps<typeof MyComponent.propTypes>) {
 // ...
}

MyComponent.propTypes = {
  id: PropTypes.number,
  name: PropTypes.string,
};

JSDoc example

import PropTypes from "prop-types";

/** @param {PropTypes.InferProps<typeof MyComponent.propTypes>} props */
function MyComponent ({ id, name }) {
  // ...
}

MyComponent.propTypes = {
  id: PropTypes.number,
  name: PropTypes.string,
};