proptypes-parser
PropTypes parser / generator for React and React Native components with GraphQL-like syntax.
Don't you just hate writing PropTypes for React components?
proptypes-parser
is cleaner, easier and less error prone way to define your PropTypes for both React and React Native applications.
It uses GraphQL schema like syntax to define PropTypes in string.
It also allows Type Composition via named definitions and spread operator ...
.
Install
npm install --save proptypes-parser
proptypes-parser
provides a default parser as parsePropTypes
.
Now ; const propTypes =
Alternatively, if you like template literals,
; const propTypes = PT`{ number: Number string: String!}`
Advanced Usage
in proptypes.js
.
;; // Provide PropTypes to the parser (Required).// Also, provide any custom type definitions (Optional).const parsePropTypes = ; ;
in component.js
.
const propTypes = ;
is equivalent to
const propTypes = number: PropTypesnumber string: PropTypesstringisRequired boolean: PropTypesbool function: PropTypesfuncisRequired date: PropTypesisRequired object: PropTypesobjectisRequired shape: PropTypesshape nested: PropTypesnumber array: PropTypes must: PropTypesboolisRequired isRequired array: PropTypesisRequired arrayOfObjects: PropTypes node: PropTypesnode element: PropTypeselementisRequired message: PropTypesisRequired any: PropTypesanyisRequired optionalUnion: PropTypes union: PropTypesisRequired;
How wonderful!
Composition via named definition
Compose types with named definitions and spread operator ...
.
// Define 'Car' type.const carPropTypes = ; // Use previously defined 'Car' type.const garagePropTypes = ; // Use spread operator on 'Car' type.const carWithMakePropTypes = ;
addType(name, type)
Add new types to the type dictionary.
// Add class instance type. {}parsePropTypes; // Add propTypes definition.// Same as named definition.const carPropTypes = ;parsePropTypes; // Add React.PropTypes type.const newsOrPhotosEnum = PropTypes;parsePropTypes; // Use above types.;
Enums
Currently, Enums are not supported. However, you can do this instead:
// Provide type extensions for this parser.const parsePropTypes = ; const propTypes = ;
or
// Provide local one-time type extensions.const propTypes = ;
Examples
See test.
Production Use
Use babel-plugin-transform-react-remove-prop-types to strip propTypes from your react components.
TODO
- Support Enums:
value: ['News', 'Photos']
- More extensive validations
- Babel plugin
- Webpack plugin
- PRs are welcome!
License
The MIT License (MIT)
Copyright (c) 2016 Joon Ho Cho
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.