Skip to content

Instantly share code, notes, and snippets.

@gund
gund / extend-decorator.ts
Last active January 24, 2019 17:30
Easily extend decorator configuration by producing new decorators
type DecoratorFactory<T, D extends Function> = (config: T) => D;
type PropertyDecoratorFactory<T> = DecoratorFactory<T, PropertyDecorator>;
function extendDecoratorConfig<C>(decoratorFactory: PropertyDecoratorFactory<C>) {
return <C_CUSTOM = {}, C_NEW extends C = C>(
configFactory: (config: C & C_CUSTOM) => C_NEW,
) => (userConfig?: C & C_CUSTOM) => decoratorFactory(configFactory(userConfig));
}
function Property(config?: { required?: boolean }): PropertyDecorator {
@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
@gund
gund / actions-helpers.md
Created January 16, 2018 11:21
Action creator factories made easy with decorators using Typescript

Actions Helpers

This is a draft idea to reduce boilerplate code when writing action creators for redux.

It simplifies the proces to bare minimum and provides shourtcuts for comminly used patterns like async action (where for one action you need to have 2 more that represent success or failure).

Draft

Interface Definitions

@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 / 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 / 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 / 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 / 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 / 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 / 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;