Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
Created November 21, 2016 14:24
Show Gist options
  • Save mgtitimoli/e4c7e1d8fc00e02f66441d637418a31f to your computer and use it in GitHub Desktop.
Save mgtitimoli/e4c7e1d8fc00e02f66441d637418a31f to your computer and use it in GitHub Desktop.
shapeOf propType
import {PropTypes} from 'react';
const setValueOn = (keys, object, value) => keys.reduce(
(result, curKey) => Object.assign(result, {[curKey]: value}),
object
);
const shapeOf = (type, keys) => PropTypes.shape(
setValueOn(keys, {}, type)
);
// Usage
// -----
//
// shapeOf(PropTypes.bool, ['field1', 'field2', 'field3'])
//
// is the same as:
//
// PropTypes.shape({
// field1: PropTypes.bool,
// field2: PropTypes.bool,
// field3: PropTypes.bool,
// })
export default shapeOf;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment