Skip to content

Instantly share code, notes, and snippets.

@mattiamanzati
Created July 14, 2017 15:05
Show Gist options
  • Select an option

  • Save mattiamanzati/7596e90d8a9214cea278f66c005602b1 to your computer and use it in GitHub Desktop.

Select an option

Save mattiamanzati/7596e90d8a9214cea278f66c005602b1 to your computer and use it in GitHub Desktop.
import { inject } from "mobx-react";
import * as React from "react";
import { ObjectOmit } from "typelevel-ts"; // We <3 You gcanti
function myInject<A, D extends keyof A>(
mapStoreToProps: (store: any) => Pick<A, D>,
component: React.ComponentType<A>
): React.SFC<ObjectOmit<A, D> & Partial<Pick<A, D>>>{
return inject(mapStoreToProps)(component as any)
}
const MyComp = ({ label, count }: { label: string; count: number }) =>
<p>
{label} x {count} times!
</p>;
const MyCompConnected = myInject((store: any) => ({ count: 1 }), MyComp);
const el1 = <MyCompConnected label="hello" />;
const el2 = <MyCompConnected labels="hello" />; // error! :D
const el3 = <MyCompConnected label="Hello world!" count={2} />;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment