在定义流文字类型时使用外部常量 [英] Use external constant when defining flow literal type

查看:53
本文介绍了在定义流文字类型时使用外部常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以流文字类型使用从包中导入的常量,并静态检查开关;

I would like to use an imported constant from a package in a flow literal type and static check the switch;

有没有办法做到这一点? (下面是示例)

Is there a way to do this? (example bellow)

// action/types.js
import { REHYDRATE } from 'redux-persist/constants'

export type FooBar = {
  foo: number,
  bar: string,
};

export type Action
  = { type: 'FETCH_REQUEST', data: FooBar[] }
  | { type: REHYDRATE, payload: any } // <== this do not work
  ;

// reducer.js
import { REHYDRATE } from 'redux-persist/constants'

export default function (state: State = initialState, action: Action) 
    switch (action.type) {
    case 'FETCH_REQUEST': 
        // do something with action.data
    case REHYDRATE: { // <= flow says: uncovered code
        // do something with action.payload
    default:
        return state
    }
}

推荐答案

Flow不支持在类型定义中使用包含常量的变量.

Flow does not support to use of variables containing constants in type definitions.

您必须在定义中使用字符串值本身或支持数据类型.

You have to either use the string value itself or a support data type in your definition.

这篇文章是类似的如果您也想对其进行审查,则该问题.

This post was a similar issue if you'd like to review it as well.

这篇关于在定义流文字类型时使用外部常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆