Created
February 3, 2019 20:45
-
-
Save tatchi/c2aa192fdf3fa8b3b960d879cd601493 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Views = 'All' | 'Details'; | |
| interface defaultValue<T> { | |
| _: () => T; | |
| } | |
| export function foldL<R>( | |
| fa: Views, | |
| obj: | |
| | { [K in Views]: (a: Views) => R } | |
| | { [K in Views]?: (a: Views) => R } & defaultValue<R>, | |
| ): R { | |
| return obj && fa in obj ? obj[fa]!(fa) : (obj as defaultValue<R>)._(); | |
| } | |
| const Title: React.FunctionComponent<{ view: Views }> = ({ view }) => { | |
| return ( | |
| <> | |
| {foldL<string>(view, { | |
| All: () => 'All page', | |
| Details: () => 'Details Page', | |
| })} | |
| </> | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment