Search code examples
typescriptreact-reduxredux-toolkit

Parameter 'state' implicitly has an 'any' type error when using redux toolkit with typescript


i used redux-toolkit with typescript but the state in the reducer alway has type of any and it will get error.

 [export const counterSlice = createSlice({
  name: "counter",
  initialState,
  // The `reducers` field lets us define reducers and generate associated actions
  reducers: {
    increment: (state) => {
      // Redux Toolkit allows us to write "mutating" logic in reducers. It
      // doesn't actually mutate the state because it uses the Immer library,
      // which detects changes to a "draft state" and produces a brand new
      // immutable state based off those changes
      state.value += 1;
    },
    decrement: (state) => {
      state.value -= 1;
    },
    // Use the PayloadAction type to declare the contents of `action.payload`
    incrementByAmount: (state, action: PayloadAction<number>) => {
      state.value += action.payload;
    },
  },
  // The `extraReducers` field lets the slice handle actions defined elsewhere,
  // including actions generated by createAsyncThunk or in other slices.
  extraReducers: (builder) => {
    builder
      .addCase(incrementAsync.pending, (state) => {
        state.status = "loading";
      })
      .addCase(incrementAsync.fulfilled, (state, action) => {
        state.status = "idle";
        state.value += action.payload;
      })
      .addCase(incrementAsync.rejected, (state) => {
        state.status = "failed";
      });
  },
});][1]

enter image description here

how can i fix that without set noImplicitAny is false


Solution

  • i had the same problem, and in my case it was an extension of vscode, JavaScript and TypeScript Nightly v4.8.20220519, I uninstalled it and the error is gone

    I hope my answer helps, there are extensions that alter the code in some way