I'm a newbie using flow so I might be doing some very stupid mistakes.
I want to create a Props flow type that intersects the props passed to a component, the props obtained from a redux store returned from mapStateToProps and some action creators defined on mapDispatchToProps.
All the code shown below is available on this flow try.
Let's consider that I have the following State and action creators:
State and Action Creators
// State type
type State = {
state_text: string,
state_num: number,
}
// Action type
type Action =
| { type: 'ACTION_CREATOR_1', text: string}
| { type: 'ACTION_CREATOR_2', value: number}
// action creators
const actionCreator_1 = (): Action => ({
type: 'ACTION_CREATOR_1',
text: 'some text',
})
const actionCreator_2 = (num: number): Action => ({
type: 'ACTION_CREATOR_2',
value: num + 1,
})
I followed an example on this post, and I came to something like the following to define my types:
Prop type definition
const mapStateToProps = (state: State) => {
const { state_text, state_num } = state
return { state_text, state_num }
}
const mapDispatchToProps = {
actionCreator_1,
actionCreator_2,
}
// type extract return helper
type _ExtractReturn<B, F: (...args: any[]) => B> = B
type ExtractReturn<F> = _ExtractReturn<*, F>
// set combined types
type ReduxState = ExtractReturn<typeof mapStateToProps>
type ReduxActions = typeof mapDispatchToProps
type OwnProps = {
own_string: string,
own_num: number
}
type Props = OwnProps & ReduxState & ReduxActions
With this definition, I was expecting Props to be something like this:
type DesiredProp = {|
own_string: string,
own_num: number,
state_text: string,
state_num: number,
actionCreator_1: () => Action,
actionCreator_2: (num: number) => Action,
|}
However, the following situation shows no flow errors:
const props2: Props = {
own_string: 'text', // OK - gives error if 123
own_num: 123, // OK - gives error if 'text'
state_text: 123, // NOK - should be an error
state_num: 'text', // NOK - should be an error
actionCreator_1: actionCreator_1, // OK - gives error if actionCreator_2
actionCreator_2: actionCreator_1, // NOK - should be an error
fakeActionCreator: () => {} // NOK - should be an error
}
I put all this code in this flow try to make it easier to understand and play with.
How can I achieve what I'm looking for?