Flow doesn't understand React definitions out of the box
I have a simple React component that I'm trying to type check with flow.
import React from 'react';
type MyProps = {
onOpen: (name: string) => void,
};
class MyComponent extends React.Component<void, MyProps, void> {
render() {
return (
<button onClick={this.props.onOpen('foo')}>Load</button>
);
}
}
export default MyComponent;
Just running flow check would return no error, but in Nuclide, I see the line where I have class MyComponent extends React.Component<void, MyProps, void> { is marked as not covered by flow.
If I hover over React, the type of it is any. This makes me think Flow is not grasping the type definitions of React. Is this a bug in Flow?
I don't have any complex project setup. Just a vanilla installation of flow on OSX.
This is surprising. Can you please follow https://flowtype.org/docs/react.html and say whether you get expected errors? That's the best way to test coverage. If not, we'll debug your install issue.
Looks like I'm not alone in seeing this bug. See comment from @nmote https://github.com/facebook/nuclide/issues/627
For comparison, Flow reports 100% coverage for the following (even with the usage of a thirdparty library like immutableJs):
/* @flow */
import type { Map } from 'immutable';
type Node = {
id: string,
name: string,
};
export type Graph = {
nodes: Array<Node>,
};
export type State = Map<string, Graph>;
But for a simple componenet like the following. Flow coverage reports 80% coverage with the following line marked as uncovered. Notice the type of React is any from Nuclide in the screenshot.
class MyComponent extends React.Component
/* @flow */
import React from 'react';
type MyComponentProps = {
name: string,
}
class MyComponent extends React.Component {
props: MyComponentProps;
render() {
return <div>this.props.name</div>;
}
}
This is a screenshot from Nuclide:

@cdchambers Are the red squiggly underlines from your linter?
I'm also noticing the comment difference but I'm not sure that matters.
// @flow vs /* @flow */
@kevinSuttle yeah, the red squiggle is from eslint (is wants the component to be written as a pure function as it has no state).
// @flow is not making a difference. Flow still thinks line 8 is uncovered.
@avikchaudhuri This happens with every React component I've seen in Nuclide. Everything still seems to get typechecked but the React in
class Foo extends React.Component
is marked as uncovered by flow coverage and type-at-pos reports any, as the screenshots above indicate. Hovering over Component does give a real type. Maybe it's just a subtlety of how react is modeled in Flow? It seems like everything is working but the fact that React is reported as uncovered worries people.
Sounds like a Nuclide-specific error then. Atom doesn't show this. Not sure about Sublime, VSCode or Webstorm.
It's not. It's just that Nuclide surfaces flow coverage whereas other editors do not. Go run flow coverage --color pathToFile and you'll see the same thing.
Ah, excellent observation.
Hey @cdchambers, seems like we're having difficulty wrapping our heads around the setup that you have that is leading to this error. Would you mind setting up a quick project on GitHub that replicates this with your current setup so we can look into it easier?
@thejameskyle yep, will throw a repro project together tonight.
Btw, on a somewhat related note, thank you for the comparison of Flow and TypeScript at https://www.reddit.com/r/javascript/comments/4vohr4/flow_type_030_adds_support_for_windows/
We're leaning toward Flow instead of TypeScript for a very large project (in the face of another large project already written in TS and folks are familiar with it). It would be awesome if there is a compilation of all the differences of the two type systems so people can weigh the pros and cons. Do you have something like that?
@thejameskyle Sample repro at https://github.com/cdchambers/flow-sample Thanks!
@cdchambers The reproduction has a syntax error and uses the wrong arity for React.Component (it takes three type arguments).
The original code appears to typecheck properly.
I too am seeing the coverage bug - this appears to be due to how much magic is in the React definition.

A simple way to get 100% coverage is to import { Component } from 'react' and the warning will go away. This is not an optimal fix though, since most prefer (including React docs):
class MyComponent extends React.Component {
}
over
class MyComponent extends Component {
}
Is this an issue with coverage itself or how the React type definition is set up?
This just seems to be a coverage bug. I can replicate it with other module definitions. The type of a CommonJS module should simply be an object containing all of its exports. It appears to be treated as such in almost all cases.
@STRML Yep, my experience is consistent with yours (though I hadn't noticed it in other cases). Flow reports React as having type any (which causes coverage to highlight it as untyped). However, typechecking still seems to work as expected.
It seems like a cosmetic bug but it would still be nice to get it fixed.
@avikchaudhuri Feel free to reach out to me directly if you don't think this thread has enough info.
I just retried this in Flow 0.53, with the same samples from above, making a few syntax changes for the React typing change in 0.53, and the class declaration line is now marked as covered in flow coverage reports.
Let me know if there are other specific cases where this is still marked as not covered, and I'll be happy to reopen and dig in further.
If I add
[ignore]
<PROJECT_ROOT>/node_modules/.*
to flo config then the coverage works. Also clicking on Component shows definitions My settings
[ignore]
<PROJECT_ROOT>/node_modules/.*
<PROJECT_ROOT>/dist/.*
[include]
[libs]
flow-typed
[lints]
[options]
module.name_mapper='.*\.s?css$' -> 'css-module-flow'
emoji=true
esproposal.class_static_fields=enable
esproposal.class_instance_fields=enable
esproposal.export_star_as=enable
esproposal.decorators=ignore
suppress_type=$FlowIssue
include_warnings=true
module.system=haste
module.file_ext=.scss
[strict]
:cl: