Skip to content

Instantly share code, notes, and snippets.

@rkaneko
Created March 9, 2017 09:49
Show Gist options
  • Save rkaneko/0e58c2fe5bf03d1ee7aac6d6bdfc3f5a to your computer and use it in GitHub Desktop.
Save rkaneko/0e58c2fe5bf03d1ee7aac6d6bdfc3f5a to your computer and use it in GitHub Desktop.
flowtype' Tagged Unions examples
/* @flow */
type OneAction = {|
kind: 'one',
type: 'ONE_ACTION',
one: string
|};
type TwoAction = {|
kind: 'two',
type: 'TWO_ACTION',
two: number
|};
type Action1 =
OneAction |
TwoAction
function reducer1(state: Object = {}, action: Action1) {
const {kind} = action;
if (kind === 'one') {
console.log(action.one);
} else if (kind === 'two') {
console.log(action.two);
}
return state;
}
// ---------------------
type Action2 =
{type: 'ONE', payload: {one: number}} |
{type: 'TWO', payload: {two: number}};
function reducer2(state: Object = {}, action: Action2): Object {
if (action.type === 'ONE') {
console.log(action.payload.one);
} else if (action.type === 'TWO') {
console.log(action.payload.two);
}
return state;
}