在Redux存储文件中运行Typescript状态类型不匹配错误

问题描述 投票:0回答:1

在我看来,我有所有类型的正确,但是我错过了一种不同的Reducer类型?

IinitialAssetsState'不能分配给'Reducer'类型

完整的错误:

输入'(state:{assets:never []; portfolio:never []; loading:boolean;} | undefined,action:any)=> IinitialAssetsState'不能赋值给'Reducer'类型。

参数'state'和'state'的类型是不兼容的。

输入'IinitialAssetsState | undefined'不能赋值给'{assets:never [];组合:从不[]; loading:布尔值; } |未定义”。

类型'IinitialAssetsState'不能分配给'{assets:never [];组合:从不[]; loading:布尔值; }”。

财产“资产”的类型是不相容的。

类型'IAsset []'不能分配给'never []'。

类型'IAsset'不能分配给'never'类型。

enter image description here

我的store.ts文件

import { applyMiddleware, createStore, combineReducers } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunkMiddleware from 'redux-thunk'

import { IinitialState } from './shared/types'
import { AssetsReducer } from './reducers/assets'
import { BoardReducer } from './reducers/board'

const rootReducer = combineReducers({
  AssetsReducer,
  BoardReducer
});

export const defaultInitialState = {
  AssetsReducer: { assets: [], loading: false, portfolio: [] },
  BoardReducer: { overlay: false },
}

export function initializeStore(initialState: IinitialState = defaultInitialState) {
  return createStore(
    rootReducer,
    initialState,
    composeWithDevTools(applyMiddleware(thunkMiddleware))
  )
}

AssetsReducer

import { Actions } from '../actions/assets'
import { IinitialAssetsState } from '../shared/types'

const defaultAssetsState = { assets: [], portfolio: [], loading: false };

export const AssetsReducer = (state = defaultAssetsState, action: any): IinitialAssetsState => {
  switch (action.type) {
    case Actions.GET_ALL_ASSETS: {
      const { assets } = action;
      return {
        ...state,
        assets,
        loading: false
      };
    }

    default:
      return state;
  }
};

BoardReducer

import { Actions } from '../actions/board'
import { IinitalBoardState } from '../shared/types'

const defaultBoardState = { overlay: false };

export const BoardReducer = (state = defaultBoardState, action: any): IinitalBoardState => {
  switch (action.type) {
    case Actions.SET_OVERLAY_STATE: {
      const { overlay } = action;
      return {
        overlay
      };
    }

    default:
      return state;
  }
};

我的类型文件

export interface IAsset {
  position: number;
  marketCap: number;
  name: string;
  percentage: number;
  price: number;
  currency: string;
  value: number;
}

export interface IinitialAssetsState {
  assets: IAsset[];
  portfolio: IAsset[];
  loading: boolean;
}

export interface IinitalBoardState {
  overlay: boolean;
}

export interface IinitialState {
  AssetsReducer: IinitialAssetsState;
  BoardReducer: IinitalBoardState;
}

我试过的

我为action创建了一个类型来删除any的使用,但我仍遇到相同的Typescript错误:

interface IAssetsAction {
  type: string;
  assets: IAsset[];
}

export const AssetsReducer = (state = defaultAssetsState, action: IAssetsAction): IinitialAssetsState => {
  console.log('action', action);
  switch (action.type) {
    case Actions.GET_ALL_ASSETS: {
      const { assets } = action;
      return {
        ...state,
        assets,
        loading: false
      };
    }
javascript typescript redux react-redux store
1个回答
1
投票

我相信这可能是store.ts的问题:

export const defaultInitialState = {
  AssetsReducer: { assets: [], loading: false, portfolio: [] },
  BoardReducer: { overlay: false },
}

这里的defaultInitialState.assets类型为never []。

您需要为defaultInitialState设置类型

export const defaultInitialState : IinitialState  = {
  AssetsReducer: { assets: [], loading: false, portfolio: [] },
  BoardReducer: { overlay: false },
}

编辑:也在AssetsReducerBoardReducer

const defaultBoardState : IinitalBoardState = { overlay: false };

© www.soinside.com 2019 - 2024. All rights reserved.