Skip to content

Instantly share code, notes, and snippets.

@gund
gund / iterate-and-drain.ts
Created November 23, 2021 23:38
Iterate and drain arrays in a nice and concise way using generators
function* drain<T>(arr: T[]): Generator<T> {
let item: T | undefined;
while ((item = arr.shift())) {
yield item;
}
}
const myArray = [1, 2, 3, 4, 5];
@gund
gund / treeshake-class-methods.ts
Last active May 4, 2021 10:20
Tree shakable class methods implemented via visitor pattern with native Typescript support
// First define standard JS constructor container
type Constructable<T> = new (...args: any[]) => T;
type ConstructableAbstract<T> = abstract new (...args: any[]) => T;
type InferConstructable<T> =
T extends Constructable<infer C> ? C :
T extends ConstructableAbstract<infer C> ? C : never;
// Now let's define visitor interfaces
@gund
gund / example.ts
Last active March 10, 2021 00:13
New abstract constructor type in Typescript 4.2 with support of the constructor arguments
type AsFunction<T> = T extends (...args: any) => any ? T : never;
type InferConstructor<T> = T extends { constructor: infer C }
? AsFunction<C>
: () => any;
type Constructor<T> = new (...args: Parameters<InferConstructor<T>>) => T;
type AbstractConstructor<T> = abstract new (
...args: Parameters<InferConstructor<T>>
) => T;
@gund
gund / index.ts
Last active January 26, 2021 23:15
Extendable actions service library
// Type Utils
interface DefaultChecker {
__defaultChecker: true;
}
type Default<T, D> = T | DefaultChecker extends DefaultChecker ? D : T;
type MapTo<T, E, M> = T extends E ? M : T;
type AsKeyOf<K, T> = K extends keyof T ? K : never;
type Observable<T> = {};
@gund
gund / 01-merge-types.ts
Last active November 25, 2019 16:51
Helper type to merge conflicting interfaces
/**
* Simple merge of two types where first `T1` will override second `T2`
*/
export type MergeSimple<T1, T2> = T1 & Omit<T2, keyof T1>;
/**
* Recursive merge of array of types where first types take over the last ones
*/
export type Merge<T extends any[]> = MergeRecursive<Head<T>, Tail<T>>;
@gund
gund / vscode-terminal-shortcuts.md
Created November 11, 2019 11:01
Terminal shortcuts for VSCode

To intercact with with VSCode built-in terminal as your default macos based terminal:

  {
    "key": "cmd+w",
    "command": "workbench.action.terminal.kill",
    "when": "terminalFocus"
  },
  {
 "key": "cmd+t",
@gund
gund / example.ts
Last active October 23, 2019 17:41
True Singleton Mixin
class B {
b: string;
}
class A extends asSingleton({ baseClass: B, ctorArgsFn: () => [] }) {
a: number;
}
class AsyncA extends asSingletonAsync({
baseClass: B,
@gund
gund / what-is-two-way-binding.md
Last active September 14, 2019 03:04
Two way binding in a nutshell...

In Angular

Two-way binding is actually a one way binding with changes synchronized back to the same state

@Component({
  template: `<input [(ngModel)]="value">`
  // This is syntactic sugar of the following
 template: ``
@gund
gund / event-open.ts
Last active August 14, 2019 16:34
Example of extensible type definitions in TypeScript leveraging interface and enum merging feature. REPL: https://repl.it/@gund/Extensible-type-definitions-in-TypeScript
import { registerMeta } from './event';
enum EventKind {
Open = 'open',
}
interface EventMetaRegistry {
[EventKind.Open]: EventOpenMeta;
}
@gund
gund / indexable-tracked.ts
Last active February 28, 2019 19:18
Helper to produce indexed objects from arrays by key
type GetValTypeIfNot<T, D> = T extends { [k: string]: infer V } ? (V extends D ? never : T) : never;
interface IndexableByDeletedProp {
__IndexableByDeletedProp: true;
}
type IndexedBy<TA extends Array<any>, K extends keyof T, T = TA[number]> = {
[P in T[K]]: GetValTypeIfNot<
{ [PP in keyof T]: T[K] extends P ? T[PP] : IndexableByDeletedProp },
IndexableByDeletedProp