I have this piece of code in types.ts file,
import { CategoryType } from "../../types/CategoryItemCount";
import { ModelErrorType } from "../../types/ModelError";
export const ACTIONS = {
SET_CATEGORIES: "SET_CATEGORIES",
ADD_CATEGORIS: "ADD_CATEGORIES",
UPDATE_CATEGORIES: "UPDATE_CATEGPRIES",
DELETE_CATEGORIES: "DELETE_CATEGORIES",
SET_MODEL_ERRORS: "SET_MODEL_ERRORS",
SENDING_REQUEST: "SENDING_REQUEST",
REQUEST_COMPLETED: "REQUEST_COMPLETED",
};
export type Action =
| { type: ACTIONS.SET_CATEGORIES, payload: CategoryType[] }
| { type: ACTIONS.ADD_CATEGORIES, payload: CategoryType }
| { type: ACTIONS.UPDATE_CATEGORIES, payload: CategoryType }
| { type: ACTIONS.DELETE_CATEGORIES, payload: number }
| { type: ACTIONS.SET_MODEL_ERRORS, payload: ModelErrorType[] }
| { type: ACTIONS.SENDING_REQUEST }
| { type: ACTIONS.REQUEST_COMPLETED };
export type State = {
categories: CategoryType[];
modelErrors: ModelErrorType[];
loading: boolean;
};
I am trying to use ACTIONS value in the type that is,
{ type: ACTIONS.SET_CATEGORIES, payload: CategoryType[] }
But I am getting error which says Cannot find namespace 'ACTIONS'.
. What am I doing wrong?
I tried couple of ways to fix this issue but none of them work. I am expecting to use the ACTIONS.SET_CATEGORIES
value while defining the type for the Action. Basically this is for useReducer hook.
2
Answers
So the fix was to use the enum, Something like this,
Only a type can be assigned to a type. And
ACTIONS.SET_CATEGORIES
is not a type.Rather try: