Created
March 13, 2020 01:26
-
-
Save ksaldana1/d068841830dec993a0e719fcd6fa344d to your computer and use it in GitHub Desktop.
ts_example_4
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
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