Flow not catching type errors for some unions as function arguments
Hi,
Looks like Flow 0.20.1 is having trouble type-checking unions that are parameters to polymorphic functions.
Here's an example with a function with 3 arguments: some data, a function to transform that data into props, and a react component that can use those props. The function then calls the react component, returning a react element. I inserted a mistake in the call site of this test function where the transform function actually returns the same type as the input data, rather than returning the appropriate props. This leads to wrong behavior at runtime as flagged by React's proptypes validation, and Flow should detect the type error as well.
test-lib.js
/* @flow */
import React from "react"
export type FunctionalComponent<Props> = (props: Props) => ReactElement
export type Component<Props, State> = ReactClass<any, Props, State> | FunctionalComponent<Props>
export type StatelessComponent<Props> = Component<Props, void>
type Transform<Data, Props> = (data: Data) => Props
export function test<Data, Props>(data: Data, transform: Transform<Data, Props>, component: StatelessComponent<Props>): ReactElement {
const Comp = component
const props = transform(data)
return <Comp {...props} />
}
and a test component:
test-component.js
/* @flow */
import React from "react"
export type TestProps = { pageTitleID: string }
const TestComponent = (props: TestProps): ReactElement => {
const { pageTitleID } = props
return <span pageTitleID={pageTitleID} />
}
TestComponent.propTypes = {
pageTitleID: React.PropTypes.string.isRequired
}
export default TestComponent
test.js
/* @flow */
import { test } from './test-lib'
import component from './test-component'
export type TestData = { prop: string }
const data: TestData = { prop: 'test data' }
function transform(data: TestData): TestData {
return {
prop: data.prop
}
}
const result = test(data, transform, component)
console.log('result', result)
Running under babel-node shows the broken behavior at runtime, with React warning about the wrong props shape:
$ npm install [email protected] react react-dom
$ node_modules/babel/bin/babel-node.js babel-test.js
Warning: Failed propType: Required prop `pageTitleID` was not specified in `TestComponent`.
result { '$$typeof': Symbol(react.element),
type: { [Function: TestComponent] propTypes: { pageTitleID: [Function: bound checkType] } },
key: null,
ref: null,
props: { prop: 'test data' },
_owner: null,
_store: {} }
Flow should catch this as well, but reports no errors.
Flow does report the error if I change StatelessComponent in test-lib.js to be:
export type StatelessComponent<Props> = FunctionalComponent<Props>
$ flow
test.js:15
15: const result = test(data, transform, component)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function call
6: const TestComponent = (props: TestProps): ReactElement => {
^^^^^^^^^ property `pageTitleID`. Property not found in. See: test-component.js:6
9: function transform(data: TestData): TestData {
^^^^^^^^ object type
Found 1 error
It still reports no errors if I change that type to be
export type StatelessComponent<Props> = ReactClass<void, Props, void>
Yup, I ran into this problem too, and it is affecting large portions of our codebase. Just a +1 for impact of this bug.
There should be used only AbstractComponent for defining components type https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoAlgWwA5wE4AuyYAhgM5gBKApiQMZFR5yZgBEetDbqqBAntmpgAKnhIA7MlHyYAPABESBEgBowABWbYyAPjABeMAAoAJspIAuMEpUBKQ-q1wdvKAFcJDdHAlgC1GQEihbqzjq6xqhgYOYq1rZq0f7iUjJ4mNZiktKyISph2nqqyXQsuBLUEgTWNPQEAHQAggBGQeIMAMLlvlXB4XqodrVcjQBycKbCAN6lvkFg3TiGYGU4vdVzUkTYRSsEqbkZZhZ2yZwE7nh+ckvYYNMNT7suZAC+YMC6qG+8AkKiQIEAYraZgbAkADm1BE6AIMGoAEkFNZ2ugJJCwL9UGVtoCgncNkQjMYXjoskCBsMqKMGhMpo4HlsFmCIdDYfCkQosSsyWRztRLtcwHIyBC-GyYXCEciDNNJRyZQoPl8fqgREDCZVqg0ySJBIFQckFdKuSN6g1wvqhGQGmiMQ10GQaABHdzoTimNV8A34giJUHg7Sog7ozG-XELOJWP0Boys4NgADkASjFiTWLcnm8vhSOXSmBO8VjpwpQQDsxiFyufkrMSDLms0d12mSv2xkaInDI7hgxP8QKLajzaVk6g1BJ62oIZ0jcARDXgkOMSe7vYISfUa77diAA
> Checks closing comment. AbstractComponent? That's been around for a bit. > Scrolls up: Flow 0.20.1? Oh boy. > Checks date. Yup, Dec 30, 2015.
Thanks @TrySound for looking through the old open issues 😄
This is for I have an access :) Hope this will let team to focus on more relevant issues.