Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropType for function argument type checking. #4811

Closed
zallek opened this issue Sep 8, 2015 · 8 comments
Closed

PropType for function argument type checking. #4811

zallek opened this issue Sep 8, 2015 · 8 comments

Comments

@zallek
Copy link

zallek commented Sep 8, 2015

I'd really like to be able to define the type of arguments a prop function should be called with.

class Test extends Component {
  static propTypes = {
    foo: PropTypes.func(PropTypes.number).isRequired,
  }
  ...
}

I think that this is the last missing piece we need to really define a React component interface. I might not be the only one to feel the need to comment those signatures. But having a way to do it programmatically would be much better.

class Test extends Component {
  static propTypes = {
    foo: PropTypes.func.isRequired, //signature: (id: Number)
  }
  ...
}

First attempt

I tried to create a new PropTypes able to do that by wrapping the function given as prop:

function createFuncTypeChecker(...argumentsTypeCheckers) {
  function validate(props, propName, descriptiveName, location) {
    const propValue = props[propName];
    const propType = typeof propValue;
    if (propType !== 'function') {
      const locationName = location;
      return new Error(
        `Invalid ${locationName} \`${propName}\` of type \`${propType}\`
         supplied to \`${descriptiveName}\`, expected a function.`
      );
    }
    props[propName] = (...args) => {
      argumentsTypeCheckers.forEach((typeChecker, i) => {
        const error = typeChecker(args, i, `${descriptiveName} arguments`, location);
        if (error instanceof Error) {
          handleArgumentsTypeError(error);
        }
      });
      propValue.apply(this, args);
    };
    return null;
  }
  return createChainableTypeChecker(validate);
}

It's working but I'm receiving the pretty straightforward warning message:
Warning: Don't set .props.foo of the React component. Instead, specify the correct value when initially creating the element or use React.cloneElement to make a new element with updated props..
Yeh, mutating the prop is not a nice way but that's the only way I found without modifying React core.

@sophiebits
Copy link
Collaborator

This won't work in 0.14 where we actually freeze the props object. I'll leave this open for now in case we want to discuss more (@sebmarkbage) but this is probably out of scope for React and would be more appropriate as a general JS type checker, perhaps one that could integrate with Flow.

@jimfb
Copy link
Contributor

jimfb commented Feb 10, 2016

Looks like there isn't any more discussion on this topic, out of scope, more appropriate for a general type checker.

@jimfb jimfb closed this as completed Feb 10, 2016
@jitcoder
Copy link

+1

@shiraze
Copy link

shiraze commented Apr 15, 2020

I think this is an excellent idea when PropTypes are used for type checking.
Clearly when using TypeScript or Flow this won't be a problem, but in that case there's no need for PropTypes

@LRNZ09
Copy link

LRNZ09 commented Aug 4, 2020

It would be nice to have this feature working with InferProps too

@albcl
Copy link

albcl commented Mar 11, 2021

I think this is an excellent idea when PropTypes are used for type checking.
Clearly when using TypeScript or Flow this won't be a problem, but in that case there's no need for PropTypes

I don't see why you wouldn't have PropTypes when also using TypeScript. Those PropTypes will validate at runtime while TypeScript will just do it when compiling. For me, they seem to do very different things.

I know this was closed years ago, but I still find this a really useful request. I think I'd be useful to give it a second thought at this whole idea.

@WagnerMoreira
Copy link

typescript

@anovazzi1
Copy link

i think it's a good idea, because of custom elements using react

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants