Skip to content

Instantly share code, notes, and snippets.

View charlypoly's full-sized avatar
🚀
Building thriving devtools

Charly Poly charlypoly

🚀
Building thriving devtools
View GitHub Profile
@charlypoly
charlypoly / retool-typescript-control-flow-analysis-8.ts
Created November 29, 2021 11:08
TypeScript lodash's isString()
function isString(value?: any): value is string;
function isPremiumUser(user: User): user is PremiumUser {
return user.plan === 'premium'
}
function getPremiumOptions(user: User | PremiumUser): PremiumOptions | null {
if (isPremiumUser(user)) {
user
// `user` is of type `PremiumUser`
@charlypoly
charlypoly / retool-typescript-control-flow-analysis-6.ts
Created November 29, 2021 11:05
TypeScript type guard definition
function isPremiumUser(user: User): user is PremiumUser {
return user.plan === 'premium'
}
type UUID = string;
interface User {
id: UUID;
first_name: string;
last_name: string;
plan: 'free' | 'premium'
}
interface PremiumOptions {
@charlypoly
charlypoly / retool-typescript-control-flow-analysis-4.ts
Created November 29, 2021 10:59
TypeScript Control Flow Analysis indirections support
function doSomething(x: string | number | boolean) {
const isString = typeof x === "string"
const isNumber = typeof x === "number"
const isStringOrNumber = isString || isNumber
if (isStringOrNumber) {
x
// `x` is of type `string | number`
if (typeof x === "number" && x > 0) {
@charlypoly
charlypoly / retool-typescript-control-flow-analysis-3.ts
Created November 29, 2021 10:56
TypeScript `instanceof` and `typeof`
import { format as fmtDate, parseISO } from 'date-fns'
function formatDate(date: Date | string | number, format = 'dd-mm-YYYY') {
let d = null
if (typeof date === 'string') {
date
// `date` is of type `string`
d = parseISO(date)
@charlypoly
charlypoly / retool-typescript-control-flow-analysis-2.ts
Created November 29, 2021 10:52
TypeScript equality narrowing
function example(x: string | number, y: string | boolean) {
if (x === y) {
x
// `x` is of type `string`
y
// `y` is of type `string`
} else {
x
// `x` is of type `string | number`
@charlypoly
charlypoly / retool-typescript-control-flow-analysis-1.ts
Created November 29, 2021 10:50
TypeScript truthiness narrowing
interface Person {
name: string
address?: {
city: string
zipcode: string
}
}
function getCity(p: Person): string | undefined {
if (p.address) {
type Mutation {
update_user(id: String!, user: UserInputType): User
}
input UserInputType {
first_name: String
last_name: String
picture_path: String
job_title: String
email: String
@charlypoly
charlypoly / apollo-form-user.ts
Last active June 6, 2018 12:49
Apollo Form User demo
import * as React from 'react';
/* imports ... */
import { client } from '../../core/apollo';
const jsonSchema: JSONSchema6 = require('./core/apollo-form-json-schema.json');
// this is common for the whole application, brang here for the example.
const AlineSidebarForm = configure<ApolloFormMutationNames>({
client,
jsonSchema,
theme: alineSidebarTheme