Skip to content

Instantly share code, notes, and snippets.



Last active May 5, 2020
What would you like to do?
RemoteData for TypeScript. Read more about how to use this pattern here:
type RemoteData<E, D> =
| { type: "NOT_ASKED" }
| { type: "LOADING" }
| { type: "FAILURE"; error: E }
| { type: "SUCCESS"; data: D };
function foldRemoteData<R, E, D>(
remoteData: RemoteData<E, D>,
notAsked: () => R,
loading: () => R,
failure: (error: E) => R,
success: (data: D) => R
): R {
switch (remoteData.type) {
case "NOT_ASKED":
return notAsked();
case "LOADING":
return loading();
case "FAILURE":
return failure(remoteData.error);
case "SUCCESS":
return success(;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment