代码之家  ›  专栏  ›  技术社区  ›  davidesp

流-无法获取动作索引因为缺少属性索引

  •  0
  • davidesp  · 技术社区  · 6 年前

    我有以下代码:

    src公司\类型.js

    export type TLoadIndex = { type: string, index: number }
    export type TLoadAll = { type: string }
    export type TDeleteAll = { type: string }
    export type TAction = TLoadIndex | TLoadAll | TDeleteAll;
    
    export type TPlane = {
        title?: string,
        caption?: string,
        text?: string,
        image: string,
    };
    

    src\存储\平面\减速器.js

    import planeList from './planeList';
    import { LOAD_INDEX, LOAD_ALL, DELETE_ALL } from './actions';
    import type { TLoadIndex, TLoadAll, TDeleteAll, TAction, TPlane } from '../../types';
    
    export default function (currentList: TPlane[] = [], action: TAction): TPlane[] {
        let newList: TPlane[] = currentList;
        switch (action.type) {
            case LOAD_INDEX:
                if (planeList[action.index])
                    newList = [...currentList, planeList[action.index]];
                break;
            case LOAD_ALL:
                newList = planeList;
                break;
            case DELETE_ALL:
                newList = [];
                break;
        }
        return newList;
    }
    

    我的问题是: 当我运行以下命令时:

    > npm run flow
    

    我得到以下信息 flow 错误:

    Error -------------- src/store/plane/reducer.js:9:25
    
    Cannot get action.index because:
     - property index is missing in TDeleteAll [1].
     - property index is missing in TLoadAll [1].
    
     [1]  5| export default function (currentList: TPlane[] = [], action: TAction): TPlane[] {
          6|        let newList: TPlane[] = currentList;
          7|        switch (action.type) {
          8|                case LOAD_INDEX:
          9|                        if (planeList[action.index])
         10|                                newList = [...currentList, planeList[action.index]];
         11|                        break;
         12|                case LOAD_ALL:
    
    
    Error -------------- src/store/plane/reducer.js:10:49
    
    Cannot get action.index because:
     - property index is missing in TDeleteAll [1].
     - property index is missing in TLoadAll [1].
    
     [1]  5| export default function (currentList: TPlane[] = [], action: TAction): TPlane[] {
          6|        let newList: TPlane[] = currentList;
          7|        switch (action.type) {
          8|                case LOAD_INDEX:
          9|                        if (planeList[action.index])
         10|                                newList = [...currentList, planeList[action.index]];
         11|                        break;
         12|                case LOAD_ALL:
         13|                        newList = planeList;
    

    关键是我不想添加属性: index 以下类型: { TLoadAll, TDeleteAll } TLoadIndex .

    问题可能在于 在内部与 switch .

    你知道怎么做吗?

    谢谢!

    1 回复  |  直到 6 年前
        1
  •  1
  •   frontendgirl    6 年前

    这个错误的主要原因是当前的操作类型注释太宽( string

    1) 向中的操作类型添加窄注释 actions . 据推测 行动

    // @flow
    export const LOAD_INDEX = 'LOAD_INDEX';
    export const LOAD_ALL = 'LOAD_ALL';
    export const DELETE_ALL = 'DELETE_ALL';
    
    

    export const LOAD_INDEX: 'LOAD_INDEX' = 'LOAD_INDEX';
    export const LOAD_ALL: 'LOAD_ALL' = 'LOAD_ALL';
    export const DELETE_ALL: 'DELETE_ALL' = 'DELETE_ALL';
    

    2) 向中的操作类型添加更窄的批注 types.js .

    import { LOAD_INDEX, LOAD_ALL, DELETE_ALL } from './actions';
    export type TLoadIndex = { type: typeof LOAD_INDEX, index: number }
    export type TLoadAll = { type: typeof LOAD_ALL}
    export type TDeleteAll = { type: typeof DELETE_ALL}
    export type TAction = TLoadIndex | TLoadAll | TDeleteAll;
    
    export type TPlane = {
        title?: string,
        caption?: string,
        text?: string,
        image: string,
    };