Last active
February 24, 2017 21:55
-
-
Save rendro/4c9f4ddea8133bbddc3df427a8f62b9e to your computer and use it in GitHub Desktop.
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 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