Skip to content

Instantly share code, notes, and snippets.

@nmn nmn/$HOC.ts
Created Mar 4, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.