PropTypes and Flow cover similar things but are using different approaches. PropTypes can give you warnings during runtime, which can be helpful to quickly find malformed responses coming from a server, etc. However, Flow seems to be the future and with concepts like generics is a very flexible solution. Also the autocompletion offered by Nuclide is a big plus for Flow.
My question now is which is the best way to go, when starting a new project. Or could it be a good solution to use both, Flow and PropTypes? The problem with using both is that you write a lot of duplicate code. This is an example of a music player app I wrote:
export const PlaylistPropType = PropTypes.shape({ next: ItemPropTypes, current: ItemPropTypes, history: PropTypes.arrayOf(ItemPropTypes).isRequired }); export type Playlist = { next: Item, current: Item, history: Array<Item> };
Both definitions basically contain the same information and when the data type is changed, both definitions need to be updated.
I found this babel plugin to convert type declarations to PropTypes, which might be a solution.
As of React v15. 5 PropTypes are deprecated in the React package and have been given a package of their own. Change is an inevitable part of life.
One of the most important things when building React application is to make sure that the components receive correct props. Passing wrong props leads to bugs and unexpected behavior, so it's a good idea to warn developers about this as early as possible.
Flow. Flow is a static type checker for your JavaScript code. It is developed at Facebook and is often used with React. It lets you annotate the variables, functions, and React components with a special type syntax, and catch mistakes early. You can read an introduction to Flow to learn its basics.
Props are required in TypeScript In the prop-types package, all props are optional by default. To make a prop required, you will have to use . isRequired explicitly. With TypeScript, that is not the case.
One year after asking this question, I wanted to give an update about how my experiences with this problem.
As Flow evolved a lot, I started typing my codebase with it and did not add any new PropType definitions. So far, I think this is good way to go, because as mentioned above, it allows you to not only type props but other parts of your code, too. This comes in really handy for example when you have a copy of you props in the state, that can be modified by the user. Also, auto-completion in IDEs is an awesome gain.
Automatic converters in one or the other direction didn't really take off. So, for new projects, I would now really recommend using Flow over PropTypes (in case you don't want to do the typing twice).
Other than both belonging to the very wide field of type checking, there's not really much similarity between the two.
Flow is a static analysis tool which uses a superset of the language, allowing you to add type annotations to all of your code and catch an entire class of bugs at compile time.
PropTypes is a basic type checker which has been patched onto React. It can't check anything other than the types of the props being passed to a given component.
If you want more flexible typechecking for your entire project then Flow/TypeScript are appropriate choices. So long as you are only passing annotated types into components, you won't need PropTypes.
If you just want to check prop types, then don't over-complicate the rest of your codebase and go with the simpler option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With