Reactjs Flowtype枚举操作重复

Reactjs Flowtype枚举操作重复,reactjs,redux,flowtype,Reactjs,Redux,Flowtype,在我的react/redux应用程序中使用流检查所有内容仍然很困难 键入我的操作时,我得到: type MessageSentAction = { type: 'MSG_SENT', message: string, } type Action = MessageSentAction | AnotherAction; 没有问题,但是我想用一个常量替换MSG_SENT类型 const actionTypes = { MESSAGE_SENT = 'MSG_SENT'} ... type

在我的react/redux应用程序中使用流检查所有内容仍然很困难

键入我的操作时,我得到:

type MessageSentAction = { type: 'MSG_SENT', 
  message: string,  
}
type Action = MessageSentAction | AnotherAction;
没有问题,但是我想用一个常量替换MSG_SENT类型

const actionTypes = { MESSAGE_SENT = 'MSG_SENT'}
...
type MessageSentAction = { type: ActionTypes.MESSAGE_SENT, 
  message: string,  
}
我怎样才能让它工作?我不希望操作字符串在操作、减缩器和类型之间重复。。。 上面的示例给出了一个错误:“在/作为流类型注释中使用的值不合格。”

您可以使用运算符


刚刚找到它。。。供参考:

const ACTION_TYPES = { MESSAGE_SENT: 'MSG_SENT', TEST: 'test'};
type ActionType = $Values<typeof ACTION_TYPES>;

type MessageSentAction = { type: ActionType, 
  message: string,  
}

function test(t: MessageSentAction) {
  t.type = 'MSG_SENT';
}
const ACTION_TYPES={MESSAGE_SENT:'MSG_SENT',TEST:'TEST'};
类型ActionType=$value;
类型MessageSentAction={type:ActionType,
消息:string,
}
功能测试(t:MessageSentAction){
t、 类型='MSG_SENT';
}
当将t.type更改为不在ACTION\u TYPES值中的内容时,我将得到一个流错误


这与前面的答案不同,在前面的答案中,任何给定的字符串都可以

不完全相同。。。当将所有类型定义为类型:“MSG_SENT”时,在指定给定类型以外的其他值时,我会收到一个错误。当使用typeof。。。我还可以将type定义为type:stringOK,这在问题中并不清楚。但我看到你已经找到了解决办法,干杯
const ACTION_TYPES = { MESSAGE_SENT: 'MSG_SENT', TEST: 'test'};
type ActionType = $Values<typeof ACTION_TYPES>;

type MessageSentAction = { type: ActionType, 
  message: string,  
}

function test(t: MessageSentAction) {
  t.type = 'MSG_SENT';
}