Skip to content

Instantly share code, notes, and snippets.

@tatchi
Created February 3, 2019 20:45
Show Gist options
  • Select an option

  • Save tatchi/c2aa192fdf3fa8b3b960d879cd601493 to your computer and use it in GitHub Desktop.

Select an option

Save tatchi/c2aa192fdf3fa8b3b960d879cd601493 to your computer and use it in GitHub Desktop.
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