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

Custom propTypes validators #1

Closed
eps1lon opened this issue Sep 17, 2019 · 2 comments · Fixed by #10
Closed

Custom propTypes validators #1

eps1lon opened this issue Sep 17, 2019 · 2 comments · Fixed by #10

Comments

@eps1lon
Copy link
Contributor

eps1lon commented Sep 17, 2019

How could I allow custom validators that cannot be expressed with static types?

For example certain components might map+clone their children in which case React.Fragment blocks this composition. I would like to validate that none of the children is a React.Fragment which is currently impossible to express with TypeScript and needs a custom propTypes validator.

@merceyz
Copy link
Owner

merceyz commented Nov 17, 2019

Turns out I wasn't watching this repository, so sorry about the slow response.


This could be done by adding a tag to the JSDoc of the prop you want this validator on and a hook into the generate function to handle said tag. There is currently no way to hook into the generate function, but i'll implement it if you want to take this route.

@eps1lon
Copy link
Contributor Author

eps1lon commented Mar 22, 2020

Currently looking at this again. I wouldn't want to pollute the JSDoc with custom tags. I think a simple mapping pipeline would be sufficient generate(theTextTypescriptToPropTypesGenerated, thePropNode): string.

For our use case I would move the text specifying that we need an element accepting a ref to the jsdoc and then check in the generate pipelin if the jsdoc contains this hint. If so then return elementTypeAcceptingRef.

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

Successfully merging a pull request may close this issue.

2 participants