Skip to content

Instantly share code, notes, and snippets.


Babak B. babakness

View GitHub Profile
babakness / stdlib.ts
Created Jun 30, 2018 — forked from KiaraGrouwstra/stdlib.ts
Type-level standard library for TypeScript
View stdlib.ts
// NOTE: code now moved to
// older revision left here, but it no longer runs well in TS Playground
export type Obj<T> = { [k: string]: T };
export type NumObj<T> = { [k: number]: T };
// export type List = ArrayLike; // no unapplied generic types :(
export type List<T> = ArrayLike<T>;
// progress:
export function force<T, V extends T>() {}
View simple-flatten-type.ts
type Flatten<T> = T extends any[] ? T[number] : T;
// If you want to flatten no lower than one level
type FlattenArray<T> = T extends any[][] ? T[number] : T;
babakness / sample-pipe.ts
Created Oct 4, 2018
Sample pipe function using overloads in TypeScript
View sample-pipe.ts
// Example taken from @type/ramda at the time of this writing.
pipe<T1>(fn0: () => T1): () => T1;
pipe<V0, T1>(fn0: (x0: V0) => T1): (x0: V0) => T1;
pipe<V0, V1, T1>(fn0: (x0: V0, x1: V1) => T1): (x0: V0, x1: V1) => T1;
pipe<V0, V1, V2, T1>(fn0: (x0: V0, x1: V1, x2: V2) => T1): (x0: V0, x1: V1, x2: V2) => T1;
pipe<T1, T2>(fn0: () => T1, fn1: (x: T1) => T2): () => T2;
pipe<V0, T1, T2>(fn0: (x0: V0) => T1, fn1: (x: T1) => T2): (x0: V0) => T2;
pipe<V0, V1, T1, T2>(fn0: (x0: V0, x1: V1) => T1, fn1: (x: T1) => T2): (x0: V0, x1: V1) => T2;
View Immutable Arrays, Object in TypeScript
type ReadonlyObj<T> = { readonly [K in string]: T }
const foo: ReadonlyArray<T> = [1,2,3]
const bar: ReadonlyObj = { a: 1, b: 2, c: 3 }
babakness / no-excess-partials.ts
Last active Nov 25, 2018
A collection of partial types which do not allow for excess properties.
View no-excess-partials.ts
// Inspired by
export type NoExcessStrictPartial<O, P> = {
[K in keyof P]: K extends keyof O ? O[K] : never
export type NoExcessDeepPartialValueFlexType<O, P> = {
[K in keyof P]:
K extends keyof O
? NoExcessDeepPartialValueFlexType<O[K],P[K]>
: never
babakness / XorExtend.ts
Created Dec 24, 2018
generic type and typed function that conditionally extend an object if the keys / value type of the first object are not in the second
View XorExtend.ts
type XorExtend<T extends {
[K in keyof T]: K extends keyof A
? A[K] extends T[K]
? never
: T[K]
: T[K]
}, A extends object,> = T & A
declare function xorExtend<O, P extends { [K in keyof P]: K extends keyof O
? O[K] extends P[K]
babakness / use-interval.ts
Created Feb 4, 2019
Typescript version of Dan Abramov's useInterval requiring non-null value for delay
View use-interval.ts
import * as React from 'react'
const { useState, useEffect, useRef } = React
type IntervalFunction = () => ( unknown | void )
function useInterval( callback: IntervalFunction, delay: number ) {
const savedCallback = useRef<IntervalFunction| null>( null )
babakness /
Last active Dec 11, 2021
A list of commands to disable the back gesture for Chrome, FireFox, Brave, and Safari

When working with online editor, the back gesture on a Mac can become very frustrating. Here are some helpful commands disable the back gesture:

# Disable back gesture in specific apps

# Chrome
defaults write AppleEnableSwipeNavigateWithScrolls -bool FALSE
# FireFox
defaults write org.mozilla.firefox AppleEnableSwipeNavigateWithScrolls -bool FALSE
# Brave

Flattening Arbitrarily Nested Arrays in JavaScript


JavaScript's immense flexibility affords the creation of arrays nested arbitrarily. Each element in an array can be of any type and of varying depths. A function that completely flatten various nested arrays can be a useful tool.

There are two general approaches to creating this flattening function, recursion and iteration, and many variations therein. As we'll see, the most elegant looking solutions to this kind of problem tend to be recursive. Alas, recursion in JavaScript is not well optimized. On the other hand, iterative solutions in JavaScript can be more complex and require extra care with regards to mutations.


babakness / tsconfig.json
Created Dec 31, 2019
Typical tsconfig.json
View tsconfig.json
"compilerOptions": {
"baseUrl": "src",
"target": "es5",
"lib": [
"allowJs": true,