运行 进入 Redux 存储文件内的 Typescript 状态类型不匹配错误
Running into Typescript state type mismatch error inside of Redux store file
在我看来,我的所有类型都是正确的,但是我是否缺少另一种 Reducer
类型?
IinitialAssetsState' is not assignable to type 'Reducer'
完整错误:
Type '(state: { assets: never[]; portfolio: never[]; loading: boolean; } | undefined, action: any) => IinitialAssetsState' is not assignable to type 'Reducer'.
Types of parameters 'state' and 'state' are incompatible.
Type 'IinitialAssetsState | undefined' is not assignable to type '{ assets: never[]; portfolio: never[]; loading: boolean; } | undefined'.
Type 'IinitialAssetsState' is not assignable to type '{ assets: never[]; portfolio: never[]; loading: boolean; }'.
Types of property 'assets' are incompatible.
Type 'IAsset[]' is not assignable to type 'never[]'.
Type 'IAsset' is not assignable to type 'never'.
我的 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
};
}
我相信这可能是 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 };
在我看来,我的所有类型都是正确的,但是我是否缺少另一种 Reducer
类型?
IinitialAssetsState' is not assignable to type 'Reducer'
完整错误:
Type '(state: { assets: never[]; portfolio: never[]; loading: boolean; } | undefined, action: any) => IinitialAssetsState' is not assignable to type 'Reducer'.
Types of parameters 'state' and 'state' are incompatible.
Type 'IinitialAssetsState | undefined' is not assignable to type '{ assets: never[]; portfolio: never[]; loading: boolean; } | undefined'.
Type 'IinitialAssetsState' is not assignable to type '{ assets: never[]; portfolio: never[]; loading: boolean; }'.
Types of property 'assets' are incompatible.
Type 'IAsset[]' is not assignable to type 'never[]'.
Type 'IAsset' is not assignable to type 'never'.
我的 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
};
}
我相信这可能是 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 };