Skip to content

Instantly share code, notes, and snippets.

💾
Writing codes

Jed Mao jedmao

💾
Writing codes
Block or report user

Report or block jedmao

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View redux.ts
/** Essentials */
export type Primitive = string | number | boolean | bigint | symbol | undefined | null;
/** Like Readonly but recursive */
export type DeepReadonly<T> = T extends Primitive
? T
: T extends Function
? T
: T extends Date
? T
@jedmao
jedmao / onAddToCart.js
Last active Aug 27, 2019
TypeScript vs. JavaScript
View onAddToCart.js
// @ts-check
/**
* @typedef {{ items: Array<CartItem> }} Cart
* @typedef {{
* sku: string,
* name: string,
* price: number,
* color?: Color,
* size?: Size,
@jedmao
jedmao / extensions-to-language.yml
Created Aug 17, 2019
File extensions to languages
View extensions-to-language.yml
abap: ABAP
bat: Windows Bat
bib: BibTex
c: C
clj: Clojure
coffee: CoffeeScript
cpp: C++
cs: C#
cshtml: Razor page/view
css: CSS
View language_identifiers.yml
abap: ABAP
bat: Windows Bat
bibtex: BibTeX
clojure: Clojure
coffeescript: Coffeescript
c: C
cpp: C++
csharp: C#
css: CSS
diff: Diff
@jedmao
jedmao / 01-Labeled.tsx
Last active Jun 25, 2019
Example of an extensible and themeable TypeScript React component.
View 01-Labeled.tsx
import React from 'react'
export interface LabeledProps {
label: string
/**
* @default 'input'
*/
children?: React.ReactNode
/**
* @default 'label'
@jedmao
jedmao / objectFromEntries.ts
Last active Mar 1, 2019
Object.fromEntries in TypeScript
View objectFromEntries.ts
type ObjectPairs<T, X = keyof T, Y = string> = Array<[X, Y]> | Map<X, Y>
export function objectFromEntries<
T extends { [key: string]: any } = { [key: string]: any },
P extends ObjectPairs<T> = ObjectPairs<T>
>(pairs: P) {
const result = {} as T
for (const [key, value] of pairs.entries()) {
result[key] = value
}
return result
@jedmao
jedmao / AddToCartFormWithHooks.tsx
Last active Jun 12, 2019
React Hooks and Render Props in TypeScript
View AddToCartFormWithHooks.tsx
import { FC, useCallback } from 'react'
import { connect } from 'react-redux';
import addToCart from '../actions/cart'
import useAddToCart, { UseAddToCartOptions } from './useAddToCart'
interface DispatchProps {
onSubmit(options: UseAddToCartOptions): Promise<void>,
}
View config.ts
import { createNinja, Katana, Shuriken } from './entities';
export default {
Warrior: createNinja,
Weapon: () => new Katana(),
ThrowableWeapon: () => new Shuriken(),
};
@jedmao
jedmao / actions.ts
Last active Feb 21, 2019
TypeScript React Thunk Action Creator
View actions.ts
import { Action } from 'redux';
import { AppActionCreator } from '.';
interface FooAction extends Action<'FOO'> {
payload: string;
}
export const foo: AppActionCreator<FooAction> = (
bar: string,
View ResponsePayload.ts
export default interface ResponsePayload {
errors?: ResponsePayloadError[];
// tslint:disable-next-line:no-any
[key: string]: any;
}
/**
* https://jsonapi.org/format/#error-objects
*/
export interface ResponsePayloadError {
You can’t perform that action at this time.