Skip to content

Instantly share code, notes, and snippets.

@bcherny
Last active July 19, 2021 17:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcherny/0189602f376163662d4b1f4624937e85 to your computer and use it in GitHub Desktop.
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

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