Skip to content

Instantly share code, notes, and snippets.

View dfoverdx's full-sized avatar

Beth Hitch dfoverdx

View GitHub Profile
@dfoverdx
dfoverdx / Permutations.ts
Last active February 7, 2023 14:20
TypeScript Permutations Type
/**
* @document Permutations.ts
*
* I wanted to figure out, just for the challenge of it, whether I could, given an array type `A`, produce a type that
* matches any array with every element of `A` exactly once in any order. I *love* abusing the TS typing engine. It
* insulted my mother once.
*/
/**
* Returns an array type that includes every element of `T` exactly once in any order.
@dfoverdx
dfoverdx / JavaScript Members Not Bound to Instances.md
Last active June 12, 2023 08:38
Why JavaScript class members are not automatically bound to the instance of the class

It took a few years, but I finally understand why member functions of classes in JavaScript aren't automatically bound to their objects, in particular when used in callback arguments.

In most object-oriented languages, functions are members of a class--they exist in memory only once, and when they are called, this is simply a hidden argument of the function which is assigned to the object that's calling it. Python makes this explicit by requiring that the first argument of a class's member function be self (Python's equivalent of this--you can name self whatever you want, but self is the convention).

class MyClass:
@dfoverdx
dfoverdx / async-database-client.js
Last active August 17, 2023 18:04
JavaScript/TypeScript Data Access Layer that automatically asserts the connection is ready, or waits for the connection to become ready
import Client from 'some-database-api';
import { connectionInfo } from './config';
class AsyncDatabaseClient {
constructor() {
this._connection = null;
this._initialized = Client.connect(connectionInfo).then(c => this.connection = c);
return new Proxy(this, {
get(target, prop, receiver) {