├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── flow-react.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | node_modules 3 | gen -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | > **Tags:** 4 | > - [New Feature] 5 | > - [Bug Fix] 6 | > - [Breaking Change] 7 | > - [Documentation] 8 | > - [Internal] 9 | > - [Polish] 10 | > - [Experimental] 11 | 12 | **Note**: Gaps between patch versions are faulty/broken releases. 13 | **Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice. 14 | 15 | ## 0.1.1 16 | 17 | - **New Feature** 18 | - add support for `FunctionComponent`s (@gcanti) 19 | 20 | ## 0.1.0 21 | 22 | Initial release -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Giulio Canti 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Advanced static type checking for react using Flow 2 | 3 | # Motivation 4 | 5 | I consider JSX pretty handy but *not critical*. 6 | 7 | What I consider critical instead is the possibility to statically type check as much as I can and currently the possibilities of Flow are kind of limited when it comes to react. 8 | 9 | For example there's no way to add constraints to `this.props.children` (e.g. The component `A` must accept exactly 2 children: the first one must be an instance of component `B`, the second one must be an instance of component `C` or a `string` ) 10 | 11 | Another example are higher order components, even if it's a popular pattern in the community, currently Flow doesn't play well with HOCs (https://github.com/facebook/flow/issues/1964). 12 | 13 | Leaving the comfort of JSX is not a light heartedly decision but it's worth noting this kind of type checking is opt-in, you can mix and match JSX (relatively unsafe) with the vdom builder `v` (safer). 14 | 15 | **Example** 16 | 17 | Requirement. The `Button` component must have a label property and must not have any children 18 | 19 | ```js 20 | import { v, Component } from 'flow-react' 21 | import type { Vdom } from 'flow-react' 22 | 23 | class Button extends Component<{ label: string }> { 24 | render() { 25 | return v('button', null, this.props.label) 26 | } 27 | } 28 | 29 | const btn = v(Button, { label: 'Click me' }) 30 | v(Button, {}) // error: property `label` not found 31 | v(Button, { label: 'Click me' }, 'hello') // error 32 | // ^^^^^^^ string. This type is incompatible with Children = void 33 | ``` 34 | 35 | Requirement. The `ButtonWrapper` component must not have any properties and must have a single child which must be an instance of the `Button` component 36 | 37 | ```js 38 | class ButtonWrapper extends Component<$Shape<{}>, Vdom