0

I'm getting the error: Type 'undefined' is not assignable to type 'WritableDraft<LoginError> | null'. in my extraReducer as follows:

export const authSlice = createSlice({
    name: "auth",
    initialState,
    reducers: {},
    extraReducers: (builder) => {
        builder
        .addCase(login.fulfilled, (state, action) => {
            console.log(action.payload);
            state.auth.loading = false;
            state.auth.success = true;
        })
        .addCase(login.rejected, (state, action) => {
            console.log(action.payload);
            state.auth.loading = false;
            state.auth.success = false;
            state.auth.error = action.payload; //this is throwing the error
        })
    }
});

I'm not sure if this is caused by the typings in my thunk but here is my code for it:

export const login = createAsyncThunk<
    LoginResponse,
    LoginData,
    {
        rejectValue: LoginError
    }>(
    "auth/login",
    async (loginData: LoginData, thunkAPI: any) => {
        try {
            const { email, password } = loginData;
            const { data: resData } = await serveraxios.post<LoginResponse>(
                "/login",
                {
                    email: email,
                    password: password,
                },
                {
                    withCredentials: true
                }
            );
            return resData;
        }
        catch (error: any) {
            return thunkAPI.rejectWithValue({
                status: error.response.data.status,
                message: error.response.data.message,
            });
        }
    }
)
export interface LoginData {
    email: string;
    password: string;
}

type LoginResponse = {
    data: UserLogin
}

interface UserLogin {
    messsage: string;
    user: UserInterface;
}

type LoginError = {
    status: number;
    message: string;
}

How can I go about resolving the error?

code_learner93
  • 571
  • 5
  • 12

0 Answers0