Skip to content

Instantly share code, notes, and snippets.

@mvolkmann
Created May 18, 2017 20:02
Show Gist options
  • Save mvolkmann/5bcd23e6951194144bb2fa81bdd2de4b to your computer and use it in GitHub Desktop.
Save mvolkmann/5bcd23e6951194144bb2fa81bdd2de4b to your computer and use it in GitHub Desktop.
Flow type discriminating
// @flow
type Action1Type = {
type: 'A1',
payload: {
name: string
}
};
type Action2Type = {
type: 'A2',
payload: {
score: number
}
};
type ActionType = Action1Type | Action2Type;
function processAction(action: ActionType) {
switch (action.type) {
case 'A1':
console.log('name =', action.payload.name);
//console.log('score =', action.payload.score); // error
break;
case 'A2':
//console.log('name =', action.payload.name); // error
console.log('score =', action.payload.score);
break;
default:
console.log('unsupported action');
break;
}
}
processAction({type: 'A1', payload: {name: 'Matt'}});
processAction({type: 'A2', payload: {score: 100}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment