schneck
schneck

Reputation: 10827

Allow partial type

Using Flowtype together with Redux, I have a type like this:

export type MapState = {
  addresses: Address[],
  selected: Array<number>
}

and an action creator:

export const setParams = (params: any): Action => {
    return { type: actionTypes.SET_PARAMS, payload: { params };

}

In the reducer, I merge the params into the state:

export default (state: MapState = initialState, action: SetParamsAction) => {
  switch (action.type) {
    case actionTypes.SET_PARAMS: {
      return {
         ...state,
         ...action.payload.params
      }
 [...]

I'm looking for a possibility to tell Flowtype to accept params in the action creator, if it is an object consisting only of properties of MapState, so that I can get rid of the any in setParams. Any idea?

Upvotes: 0

Views: 61

Answers (1)

MichaelDeBoey
MichaelDeBoey

Reputation: 2385

You can just add a exact PossibleParams Object type like so:

type PossibleParams = {|
  addresses?: Address[],
  selected?: number[],
|};

export const setParams = (params: PossibleParams): Action => ({
  type: actionTypes.SET_PARAMS,
  payload: {
    params,
  },
});

You can check all the possibilities on flow.org/try 🙂

Upvotes: 1

Related Questions