Skip to content

Instantly share code, notes, and snippets.

@kourge
kourge / jslike.code-snippets
Created March 3, 2021 23:20
a VS Code snippet for typing in JS imports module-first, like the order in Python
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
"Named import": {
"scope": "javascript,javascriptreact,typescript,typescriptreact",
"prefix": "from",
@kourge
kourge / three-cup-recipe.md
Last active March 27, 2021 21:52
Three Cup Recipe

Three Cup (Un)chicken

This is a "dual dish"; you can leave out either the chicken or the mushrooms. Adapted from this recipe. In the vegetarian version, it is fine to add the mushrooms right before the rice wine and sugar.

Ingredients

  • Deboned chicken legs, of 1 chicken, sliced
  • King oyster mushrooms, 2 big stems, rolling-cylinder sliced
  • Ginger, 6 to 7 slices
  • Garlic, 6 to 7 cloves, flattened
@kourge
kourge / ng-lifecycle-interfaces.ts
Last active May 29, 2018 21:31
symbol interfaces in angular
// Somewhere in `@angular/core`:
export const OnInit: unique symbol = Symbol('OnInit');
export interface OnInit {
[OnInit](): void;
}
// In a component:

Dive into Ruby enumeration

Enumeration is Ruby's term for iteration, and there are significant differences in how it is handled within Ruby's existing conventions when compared to other programming languages. Let us first establish some terms:

  • Enumerable, a built-in mixin. Other languages usually call this "iterable".
  • Enumerator, a built-in class. Other languages usually call this "iterator".

Dynamic methods on a TypeScript class

Sometimes, we want to be able to generalize a class that is tailored to some static set of data, but want to do so while preserving type safety. This can be achieved in TypeScript by combining userland enums, private constructors, mapped types, and intersection types.

Individual techniques

Each of the individual techniques above are known TypeScript patterns built on

@kourge
kourge / type-pick.ts
Created March 7, 2018 20:39
TypeScript: "pick" a subset out of an interface
interface Wide {
x: number;
y: number;
z: number;
}
type Narrow = Pick<Wide, 'x' | 'y'>;
@kourge
kourge / interface-extend.ts
Created March 7, 2018 20:28
TypeScript: Extend one interface from another
interface Narrow {
x: number;
y: number;
}
interface Wide extends Narrow {
z: number;
}
@kourge
kourge / check.go
Created March 7, 2018 20:14
Go: check at compile time if struct S conforms to interface I
var _ I = S{}
@kourge
kourge / enum.ts
Last active March 23, 2017 00:05
String enums in TypeScript
export enum Event1 {
SIGN_IN_WALL = 'Sign In Wall' as any,
SIGN_UP = 'Sign Up' as any,
}
{
const x = Event1.SIGN_UP; // type = Event1
const y: number = x;
// Verdict: approach is succint, but incorrectly allows type widening to number.
}
@kourge
kourge / unit.ts
Created January 19, 2017 02:45
Type-safe units in TypeScript
namespace TypeBranding {
// This incurs no runtime cost. It is a purely compile-time construct.
type Meters = number & { __metersBrand: any };
type Miles = number & { __milesBrand: any };
function Meters(i: number): Meters { return i as Meters; }
function Miles(i: number): Miles { return i as Miles; }
let a: Meters;