how to use Object.assign with literals on a reducer with defined returning flow type

98 views Asked by At

I'm writing a reducer that will be used along with React.useReducer hook. Flow is used, but the reducer is suffering from a flow error because Object.assign({}, state ... has {} as an object literal that is incompatible with SearchLocationStateType. The only solution I've found is listing all the properties on each object literal, which is not the right way.

How could I fix this?

type SearchLocationStateType = {
    textAreaFocused: boolean,
    promptsShown: boolean,
};

export const ACTIONS = Object.freeze({
    FOCUS_SEARCH_TEXT: 'FOCUS-SEARCH-TEXT',
    BLUR_SEARCH_TEXT: 'BLUR-SEARCH-TEXT',
});

type ActionType = {
    type: $Values<typeof ACTIONS>,
};

const reducer = (state: SearchLocationStateType, action: ActionType): SearchLocationStateType => {
    switch (action.type) {
        case ACTIONS.FOCUS_SEARCH_TEXT:
            return Object.assign({}, state, {textAreaFocused: true});
        case ACTIONS.BLUR_SEARCH_TEXT:
            return Object.assign({}, state, {textAreaFocused: false});
        default:
            return state;
    }
};
0

There are 0 answers