Spencer Ahrens 63d3ea17a7 Improve flow typing
Summary:
- Properly inherit flow types from base components, including `defaultProps`
- template-ify the `Item` type as it flows from the `data` prop into `ItemComponent`

Note that for `SectionList` is is harder to do the `Item` typing because each section in the
`sections` array can have a different `Item` type, plus all the optional overrides...not sure how to
tackle that.

Reviewed By: yungsters

Differential Revision: D4557523

fbshipit-source-id: a0c5279fcdaabe6aab5fe11743a99c3715a44032
2017-02-16 19:04:04 -08:00

78 lines
1.9 KiB
JavaScript

/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
const FlatList = require('FlatList');
const React = require('react');
class MyListItem extends React.Component {
props: {
item: {
title: string,
},
};
render() {
return <span />;
}
}
module.exports = {
testBadDataWithTypicalItemComponent(): React.Element<*> {
// $FlowExpectedError - bad title type 6, should be string
const data = [{
title: 6,
key: 1,
}];
return <FlatList ItemComponent={MyListItem} data={data} />;
},
testMissingFieldWithTypicalItemComponent(): React.Element<*> {
const data = [{
key: 1,
}];
// $FlowExpectedError - missing title
return <FlatList ItemComponent={MyListItem} data={data} />;
},
testGoodDataWithGoodCustomItemComponentFunction() {
const data = [{
widgetCount: 3,
key: 1,
}];
return (
<FlatList
ItemComponent={(props: {widgetCount: number}): React.Element<*> =>
<MyListItem item={{title: props.widgetCount + ' Widgets'}} />
}
data={data}
/>
);
},
testBadNonInheritedDefaultProp(): React.Element<*> {
const data = [];
// $FlowExpectedError - bad numColumns type "lots"
return <FlatList ItemComponent={MyListItem} data={data} numColumns="lots" />;
},
testBadInheritedDefaultProp(): React.Element<*> {
const data = [];
// $FlowExpectedError - bad windowSize type "big"
return <FlatList ItemComponent={MyListItem} data={data} windowSize="big" />;
},
testMissingData(): React.Element<*> {
// $FlowExpectedError - missing `data` prop
return <FlatList ItemComponent={MyListItem} />;
},
};