Last active
July 19, 2021 17:07
-
-
Save bcherny/0189602f376163662d4b1f4624937e85 to your computer and use it in GitHub Desktop.
designing an effect system in typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very cool!