Skip to content

Instantly share code, notes, and snippets.

@bcherny
Last active July 19, 2021 17:07
Show Gist options
  • Select an option

  • Save bcherny/0189602f376163662d4b1f4624937e85 to your computer and use it in GitHub Desktop.

Select an option

Save bcherny/0189602f376163662d4b1f4624937e85 to your computer and use it in GitHub Desktop.
designing an effect system in typescript
type Effect<A> = {type: A}
interface IO<A> {}
interface NetworkIO<A> extends IO<A> {}
interface DOMMutation<A> {}
interface DOMAppend<A> extends DOMMutation<A> {}
interface DOMRemove<A> extends DOMMutation<A> {}
function request<A>(url: string): Promise<A> & Effect<NetworkIO<A>> {
return fetch(url).then(_ => _.json())
}
function render<A extends HTMLElement>(element: A): void & Effect<DOMAppend<A>> {
document.body.appendChild(element)
}
function remove<A extends HTMLElement>(element: A): void & Effect<DOMRemove<A>> {
document.body.removeChild(element)
}
@biels
Copy link
Copy Markdown

biels commented Dec 1, 2018

This is very cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment