Last active
October 6, 2016 12:22
-
-
Save cmcewen/5206ca948b9489fe02417858a228ae73 to your computer and use it in GitHub Desktop.
Casting flow types
This file contains hidden or 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
type QuestionType = { | |
answers: string; | |
}; | |
type PostType = { | |
comments: string; | |
}; | |
type ActivityItemType = { | |
type: 'question' | 'post'; | |
} & (PostType | QuestionType); | |
function getComments(post: PostType) { | |
return post.comments | |
} | |
function getAnswers(question: QuestionType) { | |
return question.answers | |
} | |
function getContent(activityItem: ActivityItemType) { | |
switch (activityItem.type) { | |
case 'post': | |
return getComments(((activityItem: any): PostType)); | |
case 'question': | |
return getAnswers(((activityItem: any): QuestionType)); | |
default: | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment