代码之家  ›  专栏  ›  技术社区  ›  Leon Gaban

在redux存储文件中遇到typescript状态类型不匹配错误

  •  0
  • Leon Gaban  · 技术社区  · 6 年前

    在我看来,我所有的类型都是正确的,但是我是否遗漏了一种不同的 Reducer 类型?

    IInitialassetsState“”不能分配给类型“Reducer”

    完全错误:

    类型'(state:assets:never[];portfolio:never[];loading:boolean;undefined,action:any)=>iInitialassetsState'不可分配给类型'reducer'。

    参数“state”和“state”的类型不兼容。

    类型“iInitialassetsState Undefined”不可分配给类型“Assets:Never[];Portfolio:Never[];Loading:Boolean;Undefined”。

    类型“iInitialassetsState”不可分配给类型“assets:never[];portfolio:never[];loading:boolean;”。

    属性“assets”的类型不兼容。

    类型“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))
      )
    }
    

    减速机

    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;
      }
    };
    

    木板减速器

    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
          };
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Sumama Waheed    6 年前

    我相信这可能是 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 },
    }
    

    编辑:也在 AssetsReducer BoardReducer

    const defaultBoardState : IinitalBoardState = { overlay: false };