Skip to content

Instantly share code, notes, and snippets.

@rendro
Last active February 24, 2017 21:55
Show Gist options
  • Save rendro/4c9f4ddea8133bbddc3df427a8f62b9e to your computer and use it in GitHub Desktop.
Save rendro/4c9f4ddea8133bbddc3df427a8f62b9e to your computer and use it in GitHub Desktop.
type TAction<T: $Subtype<string>, P> = {
type: T,
payload: P,
};
const ActionTypes = {
STRING: 'STRING',
NUMBER: 'NUMBER',
LOGIN: 'LOGIN',
LOGIN_PAGE: 'LOGIN_PAGE',
LOGIN_ASYNC: 'LOGIN_ASYNC',
};
type TUser = {
+id: string,
+name: string,
+age: number,
};
type TString = TAction<typeof ActionTypes.STRING, string>;
type TNumber = TAction<typeof ActionTypes.NUMBER, number>;
type TLogin = TAction<typeof ActionTypes.LOGIN, TUser>;
type TActions =
| TString
| TNumber
| TLogin
;
/*:: const exhaustive = (action: empty) => null; */
const reducer = (state: boolean, action: TActions): boolean => {
switch (action.type) {
case ActionTypes.STRING:
return action.payload;
case ActionTypes.NUMBER:
return action.payload;
case ActionTypes.LOGIN:
return action.payload;
default:
/*:: exhaustive(action) */
return state;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment