如何使用/定义枚举与流类型检查? [英] How to use/define Enums with Flow type checking?

查看:102
本文介绍了如何使用/定义枚举与流类型检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试迁移现有的代码库以使用Flow。由于这个项目没有启动Flow,所以我使用了一个非常典型的JS模式的枚举等。

I'm trying to migrate an existing codebase to use Flow. Since this project started without Flow, I'm using a pretty typical JS pattern for enums and such.

以下是我想要的一些定义

Here are a few definitions I want to

export const LOAN_STATUS  = {
  PENDING: 'pending',
  CURRENT: 'current',
  DUE: 'due',
  OVERDUE: 'overdue',
  PENDING_PAYMENT: 'pending_payment',
  CHARGED_OFF: 'charged_off',
  VOIDED: 'voided',
  DISPUTED: 'disputed',
  REFUNDED: 'refunded',
  SETTLED: 'settled',
}

export const ACTIVE_LOAN_STATUS = [
  LOAN_STATUS.OVERDUE,
  LOAN_STATUS.CURRENT,
  LOAN_STATUS.DUE,
  LOAN_STATUS.PENDING_PAYMENT,
]

流程运行正常,直到我导入这个文件,它说我需要添加类型注释。这似乎是奇怪的 - 为什么我必须注释完全静态和容易推断的对象?

Flow works fine until I import this file and it says I need to add type annotations. This seems odd -- why should I have to annotate objects that are entirely static and easily inferred?

有没有办法将其类型定义为静态或文字 ?

Is there any way that define its type as "static" or "literal"?

那么我就去思考我将如何添加注释。我的第一个想法只是 {[key:string]:string} Array< string> 。流程工作,但我意识到这些类型的定义是完全没有价值的。所以我尝试这个其他方法:

So then I go about thinking how I'm going to add annotations to this. My first thought is just {[key: string]: string} and Array<string>. Flow works, but I'm realizing that these type definitions are totally worthless. So then I try this other approach:

type LoanStatusValues =
  'pending' |
  'current' |
  'due' |
  'overdue' |
  'pending_payment' |
  'charged_off' |
  'voided' |
  'disputed' |
  'refunded' |
  'settled'

type LoanStatusKeys =
  'PENDING' |
  'CURRENT' |
  'DUE' |
  'OVERDUE' |
  'PENDING_PAYMENT' |
  'CHARGED_OFF' |
  'VOIDED' |
  'DISPUTED' |
  'REFUNDED' |
  'SETTLED'

type ActiveLoanStatus = 
"current" |
"due" |
"overdue" |
"pending_payment"

我使用类型注释 {[ key:LoanStatusKeys]:LoanStatusValues} Array< ActiveLoanStatus> 。但是即使这些注释也松动了这一点,这是静态的!

And I use the type annotations {[key: LoanStatusKeys]: LoanStatusValues} and Array<ActiveLoanStatus>. But even these annotations loose the fact that this is static!

似乎很奇怪,我不得不写这么多重复的代码。然后如果我想要转换为流,我实际上不能使用JS中的类型。例如我可能会这样做:

It just seems so odd that I'm having to write this much duplicate code. And then if I want to convert just to Flow I can't actually use the types in JS. For example I might do this:

if (defs.ACTIVE_LOAN_STATUS.indexOf(loan.status) !== -1) {

}

现在,如果我想使用流类型,我可以'不要这样做:

Now if I want to use Flow types, I can't do anything like this:

type ActiveLoanStatus = 
  "current" |
  "due" |
  "overdue" |
  "pending_payment"

if (loan.status isTypeOf ActiveLoanStatus) {

}

那么我应该如何使用这些静态枚举?我必须这样做错了!

So how am I supposed to use these static enums? I must be doing this wrong!

推荐答案

这是最简洁的方式来实现这一点:

Here is the most concise way to achieve this:

const activeLoanStatuses = {
  current: 'current',
  due: 'due',
  overdue: 'overdue',
  pending_payment: 'pending_payment'
};

const otherLoanStatuses = {
  pending: 'pending',
  charged_off: 'charged_off',
  voided: 'voided',
  disputed: 'disputed',
  refunded: 'refunded',
  settled: 'settled',
};

type ActiveLoanStatus = $Keys<typeof activeLoanStatuses>;
type LoanStatus = $Keys<typeof otherLoanStatuses> | ActiveLoanStatus;

const activeLoanStatusesMap: { [key: LoanStatus]: ?ActiveLoanStatus} = activeLoanStatuses;

if (activeLoanStatusesMap[loan.status]) {

}

这篇关于如何使用/定义枚举与流类型检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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