javascript - 调度操作时的流输入错误

标签 javascript flowtype

我在减少 Flow 打字错误方面取得了一些进展,但仍然停留在其中的一组错误上。这是我的 Actions 文件中的一些示例代码:

export const ADD_ERROR: 'ADD_ERROR' = 'ADD_ERROR';
export const CLEAR_ERRORS: 'CLEAR_ERRORS' = 'CLEAR_ERRORS';
export const TOGGLE_PROCESSING: 'TOGGLE_PROCESSING' = 'TOGGLE_PROCESSING';
export const RESET_STATE: 'RESET_STATE' = 'RESET_STATE';
export const CLEANUP_INPUT_COMPANY_INFO: 'CLEANUP_INPUT_COMPANY_INFO' = 'CLEANUP_INPUT_COMPANY_INFO';

type AddErrorAction = {type: typeof ADD_ERROR, payload: ValidationError};
type ClearErrorsAction = {type: typeof CLEAR_ERRORS};
type ToggleProcessingAction = {type: typeof TOGGLE_PROCESSING};
type ResetStateAction = {type: typeof RESET_STATE, userRole: Role | ''};
type CleanupInputCompanyInfoAction = {type: typeof CLEANUP_INPUT_COMPANY_INFO};

export type CustomerAction = 
     | AddErrorAction
     | ClearErrorsAction
     | ToggleProcessingAction
     | ResetStateAction
     | CleanupInputCompanyInfoAction;

这是配套的 Reducers 文件(为清楚起见进行了删减):

import { ADD_ERROR,
         CLEAR_ERRORS,
         TOGGLE_PROCESSING
         RESET_STATE,
         CLEANUP_INPUT_COMPANY_INFO } from '../actions/Customer';

export const customerReducer = (state: CustomerState, action: CustomerAction) => {
  switch (action.type) {
    case ADD_ERROR: {
      return {
        // Code here
      };
    }

    case CLEAR_ERRORS: {
      return {
        // Code here
      };
    }

    case TOGGLE_PROCESSING: {
      return {
        // Code here
      };
    }

    case UPDATE_WIZARD_INDEX: {
      return {
        // Code here
      };
    }

    case RESET_STATE: {
      return {
        // Code here
      };
    }

    case CLEANUP_INPUT_COMPANY_INFO: {
      return {
        // Code here
      };
    }

    default: {
      return state;
    }
  }
}

以上代码均未出现错误^^^。

在关联的上下文中是这段代码,它是根据此处概述的方法构建的:https://kentcdodds.com/blog/how-to-use-react-context-effectively

import React, { createContext, useReducer, useContext } from 'react';

import { customerReducer } from './reducers/Customer';
import type { CustomerAction } from './actions/Customer';
import type { UndefinedType } from '../redux/FlowTypes';
import type { ValidationError, User } from './SharedTypes';
import type { Role } from '../utils/constants';

type Dispatch = (action: CustomerAction) => void;

type CustomerState = {
  isProcessing: boolean,
  wizardIndex: number,
  validationErrors: (?ValidationError)[],
  companyName: string
};

const defaultState: FleetCustomerState = {
  isProcessing: false,
  wizardIndex: 0,
  validationErrors: [],
  companyName: ''
};

const CustomerStateContext = createContext<CustomerState>(defaultState);
const CustomerDispatchContext = createContext<Dispatch | UndefinedType>(undefined);

const CustomerProvider = ({ children }: {children: Object}) => {
  const [state: CustomerState, dispatch: Dispatch] = useReducer(customerReducer, defaultState);
  return (
    <CustomerDispatchContext.Provider value={dispatch}>
      <CustomerStateContext.Provider value={state}>
        {children}
      </CustomerStateContext.Provider>
    </CustomerDispatchContext.Provider>
  )
}

const useCustomerState = () => {
  const context = useContext(CustomerStateContext);
  if (context === undefined) {
    throw new Error('useCustomerState must be used within a CustomerProvider');
  }

  return context;
}

const useCustomerDispatch = () => {
  const context = useContext(CustomerDispatchContext);
  if (context === undefined) {
    throw new Error('useCustomerDispatch must be used within a CustomerProvider');
  }

  return context;
}

export type {CustomerState};
export {CustomerProvider, useCustomerState, useCustomerDispatch};

然后在我的组件中,我这样发送:

dispatch({type: ADD_ERROR, payload: {name, message, id}});
dispatch({type: ADD_ERROR, payload: {name, message}});
dispatch({type: TOGGLE_PROCESSING});
dispatch({type: CLEAR_ERRORS});

我在这些 dispatch 语句中遇到了很多错误。每个错误的措辞略有不同,但具有相同的模式。这里有两个例子:

Cannot call dispatch with object literal bound to action because: Either string literal TOGGLE_PROCESSING [1] is incompatible with string literal RESET_STATE [2] in property type. Or string literal TOGGLE_PROCESSING [1] is incompatible with string literal CLEANUP_INPUT_COMPANY_INFO [3] in property type.

Cannot call dispatch with object literal bound to action because: Either string literal CLEAR_ERRORS [1] is incompatible with string literal RESET_STATE [2] in property type. Or string literal CLEAR_ERRORS [1] is incompatible with string literal CLEANUP_INPUT_COMPANY_INFO [3] in property type.

我不明白如何处理这些错误消息。任何建议将不胜感激!

最佳答案

我最好的猜测: - 将类型为 UPDATE_WIZARD_INDEX 的操作添加到 CustomerAction - 使 Action 注释准确。例如,而不是

type AddErrorAction = {type: typeof ADD_ERROR, payload: ValidationError};

尝试

type AddErrorAction = {|type: typeof ADD_ERROR, payload: ValidationError|}; 对于所有 Action

关于javascript - 调度操作时的流输入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58397390/

相关文章:

javascript - 流类型: How to create an instance of an exact type from a class

javascript - 如何使用附加属性扩展 Flow 函数类型

javascript - process.stdout,tty$WriteStream。该类型与stream.Writable不兼容

javascript - react js 获取 API

javascript - 如何使用内联JS定位div的第一个字母?

javascript - 使用 document.styleSheets 检查是否加载了 css 文件

javascript - Javascript 函数内未定义的 Angular 变量?

javascript - 如何构造一个数组以首先比较然后添加输入

javascript - 流类型推断不起作用

flowtype - 参数有默认值时定义类型的语法是什么?