You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Redux Toolkit configureStore遇TypeScript类型不匹配错误求助

Redux Toolkit类型错误求助

刚结束培训,正在独自完成第一个项目,未借助培训资料。此前学习的是createStore方法,现在按Redux推荐改用configureStore,但遇到了无法解决的类型错误。

错误提示:Type '{ id: string; name: string; price: number; amount: number; }' is not assignable to type 'ShoppingState'

相关代码

Store配置代码

import { configureStore } from '@reduxjs/toolkit';
import { CartProductsSliceState } from '../interfaces/slices/cartProductsSlice.interface';
import { cartProductsSlice } from './cartRedux';

export const initialState: CartProductsSliceState =
  {
    cartProducts: [
      {
        id: '1',
        name: 'pizza',
        price: 12,
        amount: 1,
      },
    ],
  };

const store = configureStore({
  reducer: {
    cartProducts: cartProductsSlice.reducer,
  },
});

export default store;

接口定义

CartProductsSliceState 接口

import { ShoppingState } from '../../redux/mainTypes';

export interface CartProductsSliceState {
  cartProducts: ShoppingState[];
}

cartProduct 接口

export interface cartProduct {
  id: string;
  name: string;
  price: number;
  amount: number;
}

ShoppingState 接口(CartState 即 cartProduct 接口)

export interface ShoppingState {
  cartProducts: CartState;
}

不清楚错误原因,希望获得帮助。

内容的提问来源于stack exchange,提问作者Devilish138

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 01:24:29