How can I overgive an error message in redux-toolkit with typescript

Hello everyone, I am writing a register function with redux-toolkit and typescript. In the extraReducers, in the case “register.rejected”, VS-Code underlines the state.message and tells me:

The type “unknown” cannot be assigned to the type “string”.ts(2322)

I tried to say: action.payload || null, but that not works.
When I write into my function: catch (error: string), then I get the message, that type must be unknown or any.
That is my function:

 const register = createAsyncThunk('/auth/register', async (user, thunkAPI)=>{
        return await authService.register(user);
    }catch (error:any) {
      const message =
        (error.response &&
        error.message ||
      return thunkAPI.rejectWithValue(message)

Now I am writing the extraReducers:

extraReducers:(builder)=> {
      .addCase(register.pending, (state)=>{
        state.isLoading = true;
      .addCase(register.fulfilled, (state, action)=>{
        state.isLoading = false;
        state.isSuccess = true;
        state.user = action.payload;
      .addCase(register.rejected, (state, action)=>{
        state.isLoading = false;
        state.isError = true;
        state.message = action.payload;//here is the error
        state.user = null;

Thanks for your help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.