Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Ability to reflect on prop-type instances? #161

Closed
sullvn opened this issue Feb 21, 2018 · 5 comments
Closed

Ability to reflect on prop-type instances? #161

sullvn opened this issue Feb 21, 2018 · 5 comments

Comments

@sullvn
Copy link

sullvn commented Feb 21, 2018

Hello!

I'm looking for a way to reflect on generated prop-type instances. The purpose is for use in developer tooling.

The problem is that the generated instances are opaque functions with any meaningful metadata hidden in closures. I could maybe see this by design to discourage monkey-patching, but it's closing the door on some cool possibilities.

Example: https://runkit.com/awfulaxolotl/lack-of-reflection-in-prop-types
A use case: sullvn/tastes#6

So I guess my question comes down to:

  1. Could this be an addition?
  2. If I made a PR, would it be accepted?

Thanks!

@wjljack
Copy link

wjljack commented Mar 14, 2018

i need too,use in storybook knobs,reflect prop-types can auto generate knobs easily.

@cpimhoff
Copy link

cpimhoff commented May 11, 2018

Given a proptype value specified on an object, you can find the key it comes from via a brute force search. This only works for the basic prop types (ie. not things like arrayOf), and not in production, since all the functions of the prop-type package is shimmed out when the nodeEnv is production.

import PropTypes from 'prop-types';
import findKey from 'lodash/findKey';

MyButton.propTypes = {
    disabled: PropTypes.bool
    onClick: PropTypes.func.isRequired
};

function getPropTypeName(typeEntry) {
    return findKey(PropTypes, p => p === typeEntry || p.isRequired === typeEntry);
}

getPropTypeName(MyButton.propTypes.disabled);  // 'bool'
getPropTypeName(MyButton.propTypes.onClick);  // 'func'

@ljharb
Copy link
Contributor

ljharb commented May 11, 2018

https://npmjs.com/find-value-locations can help with that

@sullvn
Copy link
Author

sullvn commented May 12, 2018

@cpimhoff @ljharb Thanks for the help on a partial solution. Looking up prop types by reference is certainly better than nothing 😸

However, full reflection is unfortunately required for my usecases. This includes reflecting on nested prop types and other prop types with custom constructor arguments.

@ljharb
Copy link
Contributor

ljharb commented Feb 21, 2019

Duplicate of #60.

@ljharb ljharb closed this as completed Feb 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants