Search code examples
reactjsredux

Argument of type 'SetCityAction' is not assignable to parameter of type 'UnknownAction'. in Redux React


My code is not working. When I try to dispatch an action it gives me an error.

My code is:

  const dispatch = useDispatch();

  const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
    const cityName = event.target.value;
    dispatch(setCity(cityName));
  };
  
  
  import { SET_CITY } from './actionTypes';

// Define action interfaces
interface SetCityAction {
  type: typeof SET_CITY;
  payload: string;
}

// Combine action types into a union type
export type AppActions = SetCityAction | SetDataAction;

// Define action creators
export const setCity = (city: string): SetCityAction => ({
  type: SET_CITY,
  payload: city,
});

Error:

Argument of type 'SetCityAction' is not assignable to parameter of type 'UnknownAction'.
  Index signature for type 'string' is missing in type 'SetCityAction'.

If someone knows what is the issue then please let me know. Thank you.


Solution

  • It can be fixed as follows:

    export const setCity = (city: string) => ({
        type: SET_CITY,
        payload: city,
      });