我正在遵循react-redux的this个教程,但我卡住了.

当我try 设置为configureStore时,出现错误Property 'reducer' does not exist on type 'Reducer<Winner>'

store 是这样的:

store.ts

import { configureStore } from "@reduxjs/toolkit";
import winnerSlice from "./winnerSlice";

const store = configureStore({
  reducer: {
    winners: winnerSlice.reducer,
  }
})

export type RootState = ReturnType<typeof store.getState>

export type AppDispatch = typeof store.dispatch

export type AppStore = typeof store

下面是winnerSlice.ts:

import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { RootState } from './store';

interface Winner {
  name: string
}

const initialState: Winner = {
  name: "",
}

export const winnerSlice = createSlice({
  name: 'winner',
  initialState,
  reducers: {
    setWinner: (state, action: PayloadAction<string>) => {
      state.name = action.payload
    },
    clearWinner: (state) => {
      state.name = ""
    }
  }
})

export const { setWinner, clearWinner } = winnerSlice.actions

export const selectWinner = (state: RootState) => state.winners.value // This line is also erroneous by the way 

export default winnerSlice.reducer

我需要一个关于如何配置store 的提示.文档中的示例是不完整的.这些都没有定义,所以我被困住了:

posts: postsReducer,
comments: commentsReducer,
users: usersReducer,

推荐答案

winnerSlice减速器101是默认输出.

export default winnerSlice.reducer

更新存储创建以使用正确的Reducer值,例如,仅使用默认导入.

示例:

Store.ts

import { configureStore } from "@reduxjs/toolkit";
import winnersReducer from "./winnerSlice";

const store = configureStore({
  reducer: {
    winners: winnersReducer,
    // ... other reducer functions
  }
});

...

Typescript相关问答推荐

为什么TypScript对条件函数类型和仅具有条件返回类型的相同函数类型的处理方式不同?

调用另一个函数的函数的Typescript类型,参数相同

如何使类型只能有来自另一个类型的键,但键可以有一个新值,新键应该抛出一个错误

类型脚本接口索引签名

扩展函数签名中的参数类型,而不使用泛型

如何在使用`Next—intl`和`next/link`时导航

基于平台的重定向,Angular 为16

根据上一个参数值查找参数类型,也返回类型

将对象属性转换为InstanceType或原样的强类型方法

使某些(嵌套)属性成为可选属性

通过按键数组拾取对象的关键点

有没有可能为这样的函数写一个类型?

将布尔值附加到每个对象特性

我不明白使用打字脚本在模板中展开参考

在单独的组件中定义React Router路由

如何创建内部和外部组件都作为props 传入的包装器组件?

埃斯林特警告危险使用&as";

内联类型断言的工作原理类似于TypeScrip中的断言函数?

我如何键入它,以便具有字符串或数字构造函数的数组可以作为字符串或数字键入s或n

如何让Record中的每个值都有独立的类型?