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
// without TypeScript
import PropTypes from 'prop-types';
class Greeting extends React.Component {
render() {
return (
<h1>Hello, {this.props.name}</h1>
);
}
@charlypoly
charlypoly / example.pac
Created February 23, 2018 22:18
example.pac
function FindProxyForURL(url, host) {
// If the hostname matches, use proxy.
if (
shExpMatch(host, "*.aline.work") ||
shExpMatch(host, "aline.work") ||
shExpMatch(host, "*.ipify.org")
) {
return "PROXY xx.xx.xx.xx:8888";
}
export interface BasicUserFragment {
id: string,
first_name: string,
last_name: string,
username: string | null,
picture_path: string | null,
active: boolean,
portfolio_id: string | null,
job_title: string | null,
email: string,
@charlypoly
charlypoly / redux-form-framework.ts
Created June 6, 2018 12:27
Redux form framework
import * as React from 'react';
/* imports ... */
import { pick } from 'lodash';
class Form extends ModuleForm {
fields: FieldsDefinitions = {
id: 'none',
email: 'none',
picture_path: {
type: 'image', transformations: 'h_200,w_200,r_max,c_fill'
@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
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 / 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) {
@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-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-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) {