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 premiumColor(role: USER_ROLE) { | |
switch (role) { | |
case "PREMIUM": { | |
return "red"; | |
} | |
case "WHALE": { | |
return "blue"; | |
} | |
case "FREE": { | |
return "black"; |
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": { |
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
query AppQuery { | |
messages { | |
id | |
text | |
author { | |
__typename | |
... on User { | |
id | |
name | |
role |
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 Query { | |
messages: [Message!]! | |
} | |
type Message { | |
id: ID! | |
text: String! | |
author: MessageAuthor! | |
} |
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
const Message: React.FC<MessageProps> = ({ message }) => { | |
switch (message.author.__typename) { | |
case "User": { | |
return <div>{`${message.author.name}: ${message.text}`}</div>; | |
} | |
case "Guest": { | |
return <div>{`${message.author.placeholder}: ${message.text}`}</div>; | |
} | |
case "%other": { | |
return null; |
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
import React from "react"; | |
import { useLazyLoadQuery } from "react-relay/hooks"; | |
import { AppQuery as TAppQuery } from "./__generated__/AppQuery.graphql"; | |
import { graphql } from "babel-plugin-relay/macro"; | |
const query = graphql` | |
query AppQuery { | |
messages { | |
id | |
text |
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 Query { | |
messages: [Message!]! | |
} | |
type Message { | |
id: ID! | |
text: String! | |
author: MessageAuthor! | |
} |
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); | |
} | |
function area(s: Shape) { | |
switch (s.kind) { | |
case "square": return s.size * s.size; | |
case "rectangle": return s.height * s.width; | |
case "circle": return Math.PI * s.radius ** 2; | |
default: return assertNever(s); // error here if there are missing cases | |
} |
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
interface Square { | |
kind: "square"; | |
size: number; | |
} | |
interface Rectangle { | |
kind: "rectangle"; | |
width: number; | |
height: number; | |
} |
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
// 1) Types that have a common, singleton type property — the discriminant. | |
// In this example the "kind" property is the discriminant. | |
interface Square { | |
kind: "square"; | |
size: number; | |
} | |
interface Rectangle { | |
kind: "rectangle"; | |
width: number; |