Skip to content

Instantly share code, notes, and snippets.

@ksaldana1
Created March 13, 2020 01:26
Show Gist options
  • Save ksaldana1/d068841830dec993a0e719fcd6fa344d to your computer and use it in GitHub Desktop.
Save ksaldana1/d068841830dec993a0e719fcd6fa344d to your computer and use it in GitHub Desktop.
ts_example_4
function assertNever(x: never): never {
throw new Error("Unexpected object: " + x);
}
const Message: React.FC<MessageProps> = ({ message }) => {
switch (message.author.__typename) {
case "User": {
return <div style={{color: premiumColor(message.author.role)}}>{`${message.author.name}: ${message.text}`}</div>;
}
case "Guest": {
return <div>{`${message.author.placeholder}: ${message.text}`}</div>;
}
case "%other": {
return null;
}
default: {
assertNever(message.author);
}
}
};
function premiumColor(role: USER_ROLE) {
switch (role) {
case "PREMIUM": {
return "red";
}
case "FREE": {
return "black";
}
case "%future added value": {
return "black";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment