Created
May 18, 2017 20:02
-
-
Save mvolkmann/5bcd23e6951194144bb2fa81bdd2de4b to your computer and use it in GitHub Desktop.
Flow type discriminating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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