Skip to content

Instantly share code, notes, and snippets.

@gund
gund / example.ts
Last active Mar 10, 2021
New abstract constructor type in Typescript 4.2 with support of the constructor arguments
View example.ts
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 Jan 26, 2021
Extendable actions service library
View index.ts
// 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 Nov 25, 2019
Helper type to merge conflicting interfaces
View 01-merge-types.ts
/**
* 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 Nov 11, 2019
Terminal shortcuts for VSCode
View vscode-terminal-shortcuts.md

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 Oct 23, 2019
True Singleton Mixin
View example.ts
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 Sep 14, 2019
Two way binding in a nutshell...
View what-is-two-way-binding.md

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: `<input [ngModel]="value" (ngModelChange)="value = $event">`
@gund
gund / event-open.ts
Last active Aug 14, 2019
Example of extensible type definitions in TypeScript leveraging interface and enum merging feature. REPL: https://repl.it/@gund/Extensible-type-definitions-in-TypeScript
View event-open.ts
import { registerMeta } from './event';
enum EventKind {
Open = 'open',
}
interface EventMetaRegistry {
[EventKind.Open]: EventOpenMeta;
}
@gund
gund / indexable-tracked.ts
Last active Feb 28, 2019
Helper to produce indexed objects from arrays by key
View indexable-tracked.ts
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 / extend-decorator.ts
Last active Jan 24, 2019
Easily extend decorator configuration by producing new decorators
View extend-decorator.ts
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 / no-branching.ts
Created Jan 21, 2019
Example how you can convert data without branching
View no-branching.ts
// function with branching
function giveAppleOrOrange(condition) {
if (condition) {
return 'apple';
} else {
return 'orange';
}
}
// function without branching