Я пытаюсь правильно набирать редукционные действия с помощью Flow, но при использовании констант для типов действий неправильно выполняется проверка типов непересекающихся типов.
Я читал во многих местах, что это правильный способ сделатьно я не могу вывести константу DOUBLE
в ошибку (так что на самом деле это не означает, что оператор switch правильно печатается).
Есть ли другой способ сделать это?
// @flow
export const INCREMENT: 'INCREMENT' = 'INCREMENT';
export type IncrementAction = {|
type: typeof INCREMENT,
|};
export const DECREMENT: 'DECREMENT' = 'DECREMENT';
export type DecrementAction = {|
type: typeof DECREMENT,
|};
export const DOUBLE: 'DOUBLE' = 'DOUBLE';
export type DoubleAction = {|
type: typeof DOUBLE,
|};
// Notice I didn't include DoubleAction
export type Action = IncrementAction | DecrementAction;
type State = {|
counter: number,
|};
export const reducer = (state: State, action: Action): State => {
switch (action.type) {
case INCREMENT:
return { counter: state.counter + 1 };
case DECREMENT:
return { counter: state.counter - 1 };
// This case should fail because it's in none of the actions in the disjoint union
case DOUBLE:
return { counter: state.counter * 2 };
// This case does fail because it's a literal instead of a constants
case 'TRIPLE':
return { counter: state.counter * 3 };
default:
return state;
}
};