Skip to content

Instantly share code, notes, and snippets.

Avatar

Erik Rasmussen erikras

View GitHub Profile
@erikras
erikras / typescript-id-trick.ts
Created Nov 11, 2021
TypeScript trick to use the id type off of an object.
View typescript-id-trick.ts
function selectTask(taskId: string) {
// This is okay, but it might require refactoring if the taskId type ever changes
}
function selectTask(taskId: Task['id']) {
// Much better. We don't care what type the taskId is.
}
View machine.js
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
View rescript-trouble.md

ReScript

type event = Increment | Decrement

export type transition = (. event) => unit

export createTestMachine = (. transition: transition) => {
  // Return a function that calls the transition (contrived, I know)
  let doTransition = (event:event) => transition(. event)
View machine.js
const memeMachine = Machine({
id: 'meme',
initial: 'stateMachines',
states: {
stateMachines: {
on: {
NO_IDEA_WHAT_THEY_ARE: 'stateMachines',
OMG_THEYRE_EVERYWHERE: 'howDoesThisMemeWork'
}
},
View machine.js
const jobsMachine = Machine({
id: 'jobs',
initial: 'loading',
context: {},
states: {
loading: {
invoke: {
id: 'loadJobs',
onDone: { target: 'idle', actions: 'writeDataToContext' },
},
View machine.js
const government = Machine({
id: 'USA 🇺🇸',
initial: 'Trump',
states: {
Trump: {
on: {
INAUGURATION: 'Biden'
}
},
Biden: {
View machine.js
const machine = Machine({
id: 'labelEditing',
initial: 'idle',
context: {x:400,y:400},
states: {
idle: {
on: {
ADD_LAYER: {
target: 'selection',
actions: 'addLayer',
View machine.js
const machine = Machine({
id: 'labelEditing',
initial: 'idle',
states: {
idle: {
on: {
ADD_LAYER: {
target: 'selection',
actions: 'addLayer',
},
View machine.js
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
View typescript-challenge.ts
interface Container<V> {
get: () => V;
set: (value: V) => void;
}
function createContainer<V>(value: V): Container<V> {
let v = value;
return {
get: () => v,
set: (newV: V) => {