Created
March 4, 2017 01:30
-
-
Save nmn/72f11f6ed6a003515a8291aecbe5f941 to your computer and use it in GitHub Desktop.
A generalized type-def for Higher-Order Components in React.
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 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