Skip to content

Instantly share code, notes, and snippets.

@nmn
Created March 4, 2017 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmn/72f11f6ed6a003515a8291aecbe5f941 to your computer and use it in GitHub Desktop.
Save nmn/72f11f6ed6a003515a8291aecbe5f941 to your computer and use it in GitHub Desktop.
A generalized type-def for Higher-Order Components in React.
type HOC<InjectedProps, ExtraProps, Args: $ReadOnlyArray<*>> = <D, P, C: React$Component<D, P, any>>(
component: Class<C>,
...rest: Args
) => Class<React$Component<D, $Diff<P, InjectedProps> & ExtraProps, any>>;
declare var injectName: HOC<{name: string}, {bla: Array<number>}, [string, number]>;
type Props = {
name: string,
age: number,
junk: boolean,
}
type DefaultProps = {
junk: boolean
}
declare var Person: Class<React$Component<DefaultProps, Props, void>>;
<Person name="John Doe" age={123} />;
// $ExpectError -- missing name
// <Person age={123} />;
var PersonWithName = injectName(Person, '', 1);
<PersonWithName age={123} bla={[]} />; // No Error
// $ExpectError -- missing bla
// <PersonWithName age={123} />;
// $ExpectError -- missing age
// <PersonWithName bla={[]} />;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment