[typescript] @reduxjs/toolkit 的 store 如何定义类型?

// store.ts
import { configureStore } from "@reduxjs/toolkit"
import transformReducer from "./home/useTransform"
import showPointReducer from "./home/useShowPoint"
import measureReducer from "./home/useMeasure"
import pointReducer from "./home/useDataPoints"
import tableDataReducer from "./home/getTableData"

// store 报错
// TS4023: Exported variable  store  has or is using name  CounterState  from external module
// "E:/Codes/works/iocs-web-ceph/src/stores/home/getTableData"
// but cannot be named.
// CounterState 是下面 getTableData.ts 的 Slice 的类型
const store = configureStore({
    reducer: {
        transform: transformReducer,
        showPoint: showPointReducer,
        measure: measureReducer,
        dataPoint: pointReducer,
        tableData: tableDataReducer,
    },
})
export default store

export type RootState = ReturnType<typeof store.getState>

// ./home/getTableData.ts
import { createSlice } from "@reduxjs/toolkit"

import type { Slice } from "@reduxjs/toolkit"
import type { ITableData } from "@/apis/getList"

interface CounterState {
    rulerScaling: number
    tableData: ITableData[]
}

export const counterSlice: Slice<CounterState> = createSlice({
    name: "tableData",
    initialState: {
        rulerScaling: 1,
        tableData: [] as ITableData[]
    },
    reducers: {
        setTableData(state, action) {
            state.tableData = action.payload
        },
        setRulerScaling(state, action) {
            state.rulerScaling = action.payload
        }
    }
})
 
export const { setTableData, setRulerScaling } = counterSlice.actions

export default counterSlice.reducer

因为 getTableData 这个文件定义了counterSlice 的类型, 其他都没有定义,因为别的地方只有getTableData需要用到类型
但是store 里报错

TS4023: Exported variable  store  has or is using name  CounterState  from external module
"E:/Codes/works/iocs-web-ceph/src/stores/home/getTableData"
but cannot be named.

这个怎么解决?

阅读 510
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏