Skip to content

Instantly share code, notes, and snippets.

View bmingles's full-sized avatar

Brian Ingles bmingles

View GitHub Profile
@bmingles
bmingles / some-component.ts
Created December 12, 2017 02:44
Making Vuex stores type safe
import { Dispatcher } from './store';
export default Vue.component('some-component', {
template,
created() {
this.fetchData();
},
methods: {
/**
* Fetch our data.
/**
* Typings for Folktale data.task
*/
declare module 'data.task' {
type Action<T> = (t: T) => void;
type Func<A, B> = (a: A) => B;
class Task<E, A> {
constructor(
callback: (
declare module 'control.async' {
import Task = require('data.task');
function parallel<E1, A>(
tasks: [Task<E1, A>]
): Task<E1, [A]>;
function parallel<E1, E2, A, B>(
tasks: [Task<E1, A>, Task<E2, B>]
): Task<E1 | E2, [A, B]>;
function parallel<E1, E2, E3, A, B, C>(
@bmingles
bmingles / gist:23b90a93ed7088feb73d4d4294555f8a
Created December 10, 2018 14:43
vscode - sample launch config + preLaunchTask with watch
// launch.json
{
"type": "node",
"request": "launch",
"name": "Server build and debug",
"useWSL": true,
"program": "${workspaceFolder}/server/js/src/app.ts",
"cwd": "${workspaceFolder}/server",
"preLaunchTask": "npm-buildserver",
"outFiles": [
module 'draft-js-plugins-editor' {
import React from 'react';
import {
CompositeDecorator,
DraftEditorCommand,
Editor,
EditorProps,
EditorState
} from 'draft-js';
@bmingles
bmingles / create-app.js
Last active February 17, 2020 00:43
NodeJS script to scaffold minimal React + TypeScript app using ParcelJS
#!/usr/bin/env node
/**
* Script for scafolding a React + TypeScript app using
* ParcelJS as a bundler.
*
* - TypeScript
* - React
* - ParcelJS
* - Sass
@bmingles
bmingles / tagged-union.ts
Last active April 20, 2020 15:31
TypeScript tagged union matching with exahaustive checking
/**
* Map discriminated union based on its tag value.
* Possible tag values are mapped to expressions
* that return a transformation of the original value.
* Each value is exhaustive checked such that a mapping
* has to be provided for each one.
*
* e.g.
*
* type Union =
@bmingles
bmingles / useUpdate.fs
Created May 7, 2020 11:36
Fable MVU reducer
module Util.MVU
open Fable.Core
open Fable.React
type Dispatch<'msg> = 'msg -> unit
type Sub<'msg> = Dispatch<'msg> -> unit
type Cmd<'msg> = Sub<'msg> list
@bmingles
bmingles / router.ts
Created May 21, 2020 01:25
Strong typed routing
export interface Spec<K extends string, T> {
ctr: (raw: string) => T,
key: K,
match: RegExp
}
type Data<T, D> = {
type: T
} & { [P in keyof D]: D[P] };
@bmingles
bmingles / switchExp.ts
Last active May 25, 2020 04:14
Tagged Union Switch Expression
type Tagged<T extends string> = {
type: T
}
export function switchExp<
T extends Tagged<string>,
M extends { [P in T['type']]: T extends Tagged<P> ? (t: T) => any : never }
>(value: T, map: M): ReturnType<M[keyof M]> {
return map[value.type as keyof M](value)
}