Skip to content

Instantly share code, notes, and snippets.

@Willmo36
Created June 28, 2017 23:22
Show Gist options
  • Save Willmo36/8ab76cfeac0b4aa3fbe82589c6cc638e to your computer and use it in GitHub Desktop.
Save Willmo36/8ab76cfeac0b4aa3fbe82589c6cc638e to your computer and use it in GitHub Desktop.
/**REMOTE DATA IMPLEMENTATION */
type Idle = { key: "idle", data: null };
type Loading = { key: "loading", data: boolean };
type Success = { key: "success", data: string };
type RemoteDataState = Idle | Loading | Success;
type LoadTransition = [Idle, Loading];
type SuccessTransition = [Loading, Success];
type IdleTransition = [Success, Idle];
type RemoteDataTransition = LoadTransition | SuccessTransition | IdleTransition;
const makeRun2 = <State, Transition>() => <From extends State, To extends State>(t: [From, To] & Transition) => t[1];
const run2 = makeRun2<RemoteDataState, RemoteDataTransition>();
let idle: RemoteDataState = { key: "idle", data: null };
let loading: RemoteDataState = { key: "loading", data: true };
let success: RemoteDataState = { key: "success", data: "boo" };
let myRequest: RemoteDataState = run2([idle, loading]);
let shouldBeBool = myRequest.data;
myRequest = run2([loading, success]);
let shouldBeString = myRequest.data;
run2([loading, idle]); //this fails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment