Skip to content

Instantly share code, notes, and snippets.

@cmcewen
Last active October 6, 2016 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmcewen/5206ca948b9489fe02417858a228ae73 to your computer and use it in GitHub Desktop.
Save cmcewen/5206ca948b9489fe02417858a228ae73 to your computer and use it in GitHub Desktop.
Casting flow types
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