小编suy*_*eon的帖子

如何修复 redux-toolkit 中的打字稿错误?

我正在用打字稿做一个 redux-toolkit 教程。但我是打字稿初学者。

我不知道这里出了什么问题。请给我你的见解。

这是一条错误消息。: TS2322: 类型“number”不可分配给类型“void |” 状态| 可写草稿'。

import {CaseReducer, createSlice, PayloadAction} from "@reduxjs/toolkit";

type State = {
  value: number
}
const increment: CaseReducer<State,PayloadAction<number>> = (state, action) => state.value + action.payload; // error line

export const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    value: 0
  },
  reducers: {
    increment,
    decrement: state => {
      state.value -= 1
    },
    incrementByAmount: (state, action) => {
      state.value += action.payload
    },
  },
})

export const {increment, decrement, incrementByAmount} = counterSlice.actions;

export default counterSlice.reducer;
Run Code Online (Sandbox Code Playgroud)

typescript redux redux-toolkit

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

redux ×1

redux-toolkit ×1

typescript ×1