Replace React.PropTypes with Flow types
(forked off of #267)
I'd like to use Flow types to document my React Component's props. If it were possible to do this, then React.PropTypes would be entirely superfluous—it's a less precise way of documenting the types, and the checks are performed at runtime instead of compile time.
This would allow lots of duplicated type definitions to be removed. You can see this in the Flow example code. Looking at just the Message type, for example, its definition is duplicated four times in the six components:
(as Flow type) https://github.com/facebook/flow/blob/master/examples/flux-chat/js/components/ThreadSection.react.js https://github.com/facebook/flow/blob/master/examples/flux-chat/js/components/MessageSection.react.js
(as React type) https://github.com/facebook/flow/blob/master/examples/flux-chat/js/components/ThreadListItem.react.js https://github.com/facebook/flow/blob/master/examples/flux-chat/js/components/MessageListItem.react.js
Ideally you'd be able to write something like:
/* @flow */
type Message = {
authorName: string;
date: Date;
text: string;
};
var MessageListItem = React.createClass({
propTypes: {
message: Message
},
render: function() {}
});
Flow could use the type information for type checking, then strip it out completely for production, i.e. translate this to:
var MessageListItem = React.createClass({
render: function() {}
});
This is supposed to become easier with the introduction of class-based React components. An alternate class-based syntax:
type Props = { /*... */ }
type State = { /* ... */ }
class MyComponent extends ReactComponent<Props, State> {
render() {
/* typeof this.props is Props */
/* typeof this.state is State */
}
}
My understanding is that this is also blocked by the lack of bounded generics, so the the types of getDefaultProps, getDefaultState, setState, and setProps can be specified.
The plan is to be able to annotate the props type using Flow. It is made easier by ES6 classes since we have a nicer syntactic space for it.
class MyComponent extends React.Component {
props : Props;
state : State = { initial: 'value' };
render() {
/* typeof this.props is Props */
/* typeof this.state is State */
}
}
The syntactic solution for React.createClass is kind of messy.
var MessageListItem = React.createClass({
props: (null : ?Props),
render: function() {
if (this.props !== null) { // guard for safety
/* this.props is Props */
}
}
});
Perhaps Flow could build some special support that allows annotating props in this way but it seems like more work than it is worth, since the world is moving on to ES6 classes.
The goal is to provide a "React Modern" package which doesn't include runtime features for React.createClass/PropTypes/createElement and instead relies on language features like ES6 Classes/Flow/JSX. The other stuff will be continued to be supported in "React Classic".
If your entire codebase is Flowified, you don't need dynamic type checks.
However, PropTypes still serve the dynamic type checking scenario in the boundary to un-Flowified code. E.g. if you publish it online, others might want your type annotations for runtime type checking. However, I think that would be better handled by a generic Flow Safe or Flow Dynamic Boundaries solution rather than something React specific. Once we have that, there is no need for React PropTypes anymore.
I'll note that it's currently not possible to use React.Component classes in JSX with flow because React.createClass is currently special cased as a CustomClassT, and the only way to generate that type is with React.createClass.
EDIT: For anyone looking at this now, the above is no longer true. React.Component works great.
@danvk, you should be able to use flow type syntax with React.Component subclasses now. If that is posing any issues for you, please reopen this. Thanks for the report!
@samwgoldman can you point to an example of this? Would be much appreciated!
@yanivtal There are a bunch of examples in the tests for the new React API. Note that many of the examples are designed to fail in an expected way, but that should give you an idea.
Was the hope to be able to do away with prop types and do type annotations with flow? All the examples use React.createClass. I'm hoping to be able to use ES6 classes, specify prop types with flow and then get autocomplete through https://atom.io/packages/ide-flow. Is something like that possible today?
Ahh, I found classes.js. Looks clean.
I don't seem to be able to make it work (running Flow 0.14.0). Here's my test case:
/* @flow */
type P = { width: number; };
class Label extends React.Component {
props: P;
render() {
return <div style={{width: this.props.width * 2}}>Label</div>;
}
};
var f = <Label width="100px" height="20px" />;
I expect this to produce 2 errors in the last line: that width is expected to be a number, and that height is not supported. Instead I get "No errors!" message. Am I doing anything wrong? I also tried extending React.Component<any,P,any> -- with the same result.
@pasha-mf You need to specify the type of defaultProps as well (ref #398).
Also, passing too many props is not an error, so Flow won't complain about it.
/* @flow */
var React = require("react");
type P = { width: number; };
class Label extends React.Component {
props: P;
static defaultProps: {}; // this part is important
render() {
return <div style={{width: this.props.width * 2}}>Label</div>;
}
};
var f = <Label width="100px" height="20px" />;
Ok, but even after adding the declaration for defaultProps it still doesn't complain about width being a string...
Are you sure? I just tried and got this:
test.js|14 col 9 error| React element: Label
|| Error:
test.js|14 col 22 error| string
|| This type is incompatible with
test.js|4 col 19 error| number
||
(it's working now)
Does this work with ES6 import? I have
/* @flow */
import React from 'react';
type P = { width: number; };
class Label extends React.Component {
props: P;
static defaultProps: {}; // this part is important
render() {
return <div style={{width: this.props.width * 2}}>Label</div>;
}
}
let f = <Label width="100px" height="20px" />;
Using Flow 0.17.0 and I get No Errors.
@pasha-mf Could you shed some light on anything you did differently to get it working? I pasted your code verbatim but I get zero errors. I also had a quick discussion with @samwgoldman with my own test case (pasted below) that I just tried on an empty folder which he could also get to display the correct errors whereas I still get 0 so if anyone has experienced this and figured it out please let me know! (I'm testing with 0.18.1 but just in case I tried the last few versions as well and couldn't get anything)
My own simple case:
/* @flow */
import React from 'react';
type P = {
prop1: string;
prop2: number;
};
class Test extends React.Component {
static defaultProps: {};
props: P;
render() {
return <div>
{this.props.prop1 * this.props.prop2} // get expected error for incompatible types
</div>;
}
}
class ParentTest extends React.Component {
render() {
return <Test prop1={1} />; // hoping to get error for wrong type and missing prop
}
}
@frankychung @samwgoldman I'm also using 0.18.1 and do not get any flow warnings/errors with the example code.
However after a bit of experimentation I was able to get the correct flow error if I extend ReactComponent instead of React.Component
ie
class Label extends ReactComponent { ... }
While this type checks correctly it does not run in the browser as ReactComponent is a flow declaration and not a real class
@frankychung @cloudkite Isn't this bizarre? I just tested Franky's piece: create a new directory, do flow init in that directory, create file test.js (has to have extension .js) and paste the code into that file, and finally run flow. It gives me 3 errors, as expected (1 error in line 16 and 2 in line 23).
My Flow version is also 0.18.1 (more specifically, revision d3f6f17e07cff4afef9b48eb0b21f3557ff3cdce). I did a clean make after the pull, could this be the reason?
I really have no idea... When I wrote the comment 2 months ago I believe what made the difference was to switch from React.Component to ReactComponent. But right now both versions work fine for me, so it could have been just a coincidence.
Sorry I can't be of much help here...
Great, using ReactComponent gives me the expected errors! @pasha-mf: Thanks for checking! I tried building from latest master but no errors as well. As @cloudkite mentioned, using ReactComponent doesn't work in the browser. Could you share how you were able to switch to it?
@frankychung @samwgoldman did a bit more digging and managed to get it working if I explicitly cast
class Label extends (React.Component : typeof ReactComponent) { ... }
Which seemed strange since given https://github.com/facebook/flow/blob/master/lib/react.js#L177
This got me thinking that maybe flow isn't picking up the builtin declaration of react properly.
so I changed my .flowconfig to
[ignore]
.*/node_modules/react/.*
Which now gives the correct flow warnings without the explicit cast :+1: :fireworks:
I thought declarations took preference over node_modules?
Ah, glad you figured out the solution (adding an ignore where a declaration exists). This issue is tracked in #676.