question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for imported propTypes

See original GitHub issue

Importing prop types is a fairly common use case which doesn’t appear to be possible at this stage. Any plans to support this pattern?

Example:

// defaultPropTypes.js

import { PropTypes } from 'react';

export default {
    prop1: PropTypes.string
};
// otherPropTypes.js

import { PropTypes } from 'react';

export default {
    prop2: PropTypes.object
};
// MyComponent.jsx

import { Component } from 'react';
import defaultPropTypes from './defaultPropTypes.js';
import otherPropTypes './otherPropTypes.js';

export default class MyComponent extends Component {
    static propTypes = {
        ...defaultPropTypes,
        ...otherPropTypes
    }
    // etc...
}

Results in:

{
  "MyComponent.jsx": {
    "description": ""
  }
}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:107
  • Comments:50 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
devongovettcommented, Apr 29, 2019

I started hacking on this today and managed to get importing working for propTypes, flow, and typescript types. See https://github.com/devongovett/react-docgen/compare/typescript...devongovett:importing. I’ll open up a PR once the typescript PR (#348) is merged, since this builds on that. In the meantime, feel free to try out my branch.

Current limitations:

  • ES6 imports only. Could add CommonJS support, but it’s a bit more complicated and I’m not sure it’s worth it when most apps should be using ES6 now anyway.
  • Namespace imports are unsupported (import * as foo from '...')
  • Wildcard re-exports are unsupported (export * from '...')
  • Resolving from node_modules is supported, but flow libdefs (flow-typed folder, .js.flow files, and .d.ts files) are unsupported. Would need to reimplement a custom resolver for those.

Please let me know if you try it out and encounter bugs.

11reactions
devongovettcommented, May 4, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use PropTypes in React - freeCodeCamp
We can use PropTypes to validate any data we are receiving from props. But before using it we will have to import it...
Read more >
How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >
Need to import prop types in React? - Stack Overflow
Say I have a class and at the end I want to make it explicit in my code that it has no props...
Read more >
Typechecking With PropTypes - React
import PropTypes from 'prop-types'; class Greeting extends React.Component { render() { return ( <h1> ...
Read more >
prop-types - npm
import React from 'react'; import PropTypes from 'prop-types'; class MyComponent extends React.Component { render() { // ... do things with ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found