Skip to content

Instantly share code, notes, and snippets.

View ksaldana1's full-sized avatar

Kevin Saldaña ksaldana1

View GitHub Profile
@ksaldana1
ksaldana1 / ts_example_4_fix.ts
Created March 13, 2020 01:27
ts_example_4_fix
function premiumColor(role: USER_ROLE) {
switch (role) {
case "PREMIUM": {
return "red";
}
case "WHALE": {
return "blue";
}
case "FREE": {
return "black";
@ksaldana1
ksaldana1 / ts_example_4.ts
Created March 13, 2020 01:26
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": {
query AppQuery {
messages {
id
text
author {
__typename
... on User {
id
name
role
type Query {
messages: [Message!]!
}
type Message {
id: ID!
text: String!
author: MessageAuthor!
}
@ksaldana1
ksaldana1 / gql_example_ts_trunc_1.ts
Created March 13, 2020 01:25
gql_example_ts_trunc_1
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;
@ksaldana1
ksaldana1 / gql_ts_example_1.ts
Created March 13, 2020 01:24
gql_ts_example_1
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
@ksaldana1
ksaldana1 / example_1.graphql
Created March 13, 2020 01:22
gql_example_1
type Query {
messages: [Message!]!
}
type Message {
id: ID!
text: String!
author: MessageAuthor!
}
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
}
interface Square {
kind: "square";
size: number;
}
interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}
@ksaldana1
ksaldana1 / example_1.ts
Created March 13, 2020 01:14
example_1
// 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;