【问题标题】:React redux - object is possibly 'undefinedReact redux - 对象可能是'未定义的
【发布时间】:2019-11-06 10:52:46
【问题描述】:

我收到一个 Typescript 错误,指出我在 Redux 中使用的对象可能未定义,即使我没有说它的类型可以未定义或在任何地方设置为未定义。

/redux/globalSettings/actions.ts

import { ADD_GLOBAL_SETTINGS } from '../../config/actions';
import { AddGlobalSettingsAction } from './types';
import GlobalSettings from '../../typings/contentful/GlobalSettings';

export const addGlobalSettings = (payload: GlobalSettings): AddGlobalSettingsAction => ({
  type: ADD_GLOBAL_SETTINGS,
  payload,
});

/redux/globalSettings/reducers.ts

import { ADD_GLOBAL_SETTINGS } from '../../config/actions';
import { GlobalSettingsAction, GlobalSettingsState } from './types';

export default (
  state: GlobalSettingsState,
  action: GlobalSettingsAction,
): GlobalSettingsState  => {
  switch (action.type) {
    case ADD_GLOBAL_SETTINGS:
      return { ...action.payload };
    default:
      return state;
  }
}

/redux/globalSettings/types.ts

import { ADD_GLOBAL_SETTINGS } from '../../config/actions';
import GlobalSettings from '../../typings/contentful/GlobalSettings';

export type GlobalSettingsState = GlobalSettings;

export interface AddGlobalSettingsAction {
  payload: GlobalSettings;
  type: typeof ADD_GLOBAL_SETTINGS;
}

export type GlobalSettingsAction = AddGlobalSettingsAction;

/redux/reducer.ts

import { combineReducers } from 'redux';
import globalSettings from './globalSettings/reducers';

const rootReducer = combineReducers({
  globalSettings,
});

export type StoreState = ReturnType<typeof rootReducer>;

export default rootReducer;

/redux/index.ts

import { applyMiddleware, createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';
import rootReducer, { StoreState } from './reducer';

export const initialiseStore = (
  initialState: StoreState,
) => createStore(
  rootReducer,
  initialState,
  composeWithDevTools(applyMiddleware()),
);

我在我的 NextJS 项目中使用它,在我的 _app.js 页面的导出上使用 next-redux-wrapper NPM 包(一个 React HOC),如下所示:

export default withRedux(initialiseStore)(Page);

我在 /redux/reducer.ts 中遇到错误:Type 'undefined' is not assignable to type 'GlobalSettings'

如果我在我的一个页面上使用 globalSettings redux 状态,访问 globalSettings.fields.navigationLinks 会创建另一个 Typescript 错误,globalSettings 可能未定义。

让我发疯,我在这里做错了什么?

【问题讨论】:

  • @AluanHaddad 没有相关的内容被标记为未定义或可选的未定义。我可以发布该文件,但基于此响应,可能没有意义。
  • 好的。然后,您可能会在库中某处的某个部分中传递一个部分类型,但这只是一个猜测

标签: javascript reactjs typescript redux


【解决方案1】:

错误

我在 /redux/reducer.ts 中收到错误:类型“未定义”不可分配给类型“全局设置”

与你如何定义reducer有关

应该是

const initalState: GlobalSettingsState = {/* valid inital state */};

export default (
    state: GlobalSettingsState | undefined = initalState,
    action: GlobalSettingsAction,
): GlobalSettingsState  => {
    switch (action.type) {
        case ADD_GLOBAL_SETTINGS:
            return { ...action.payload };
        default:
            return state;
    }
}

Reducer 可以在 state 设置为 undefined 的情况下调用(初始化 state)。所以state 参数应该有undefined 作为可能的值。

【讨论】:

    猜你喜欢
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多