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)=>{
try{
return await authService.register(user);
}catch (error:any) {
const message =
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString()
return thunkAPI.rejectWithValue(message)
}
})
Now I am writing the extraReducers:
extraReducers:(builder)=> {
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.