在我看来,我所有类型都正确,但是我是否缺少另一种Reducer
类型?
IinitialAssetsState”不能分配给“ Reducer”类型
完整错误:
类型'(状态:{资产:never [];投资组合:never [];加载:布尔值;} |未定义,操作:任意)=> IinitialAssetsState'不能分配给类型'Reducer'。
参数“状态”和“状态”的类型不兼容。
类型'IinitialAssetsState |未定义”无法分配给类型“ {{assets:never [];投资组合:从不[]; loading:布尔值; } |未定义”。
类型'IinitialAssetsState'不可分配给类型'{资产:never [];投资组合:从不[]; loading:布尔值; }'。
属性“资产”的类型不兼容。
类型'IAsset []'不能分配给类型'从不[]'。
类型'IAsset'不能分配给类型'从不'。
我的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
};
}
答案 0 :(得分: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 },
}
编辑:也在AssetsReducer
和BoardReducer
const defaultBoardState : IinitalBoardState = { overlay: false };