-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
add flow type definition file #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
options?: {pure?: boolean, withRef?: boolean} | ||
) => (component: (props: P) => any) => Class<React$Component<void, $Diff<P, DP>, void>> | ||
|
||
type ConnectDisptch = <D, P, S, C: React$Component<D, P, S>, SP, Dispatch: Function>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be ConnectDispatch
?
@gaearon I tried to support the case when So far my entire type definition heavily relies on the magic type It turns out, that B has to be a strict subset of A for it to work. But to support I can still fix the type if you're willing to merge this. It's a little more wordy to write out the function version but I think people who use Flow will trade it for the improved type-safety. |
Update: One other possibility is to use a more generic type: type Connect = <D, P, S, C: React$Component<D, P, S>, SP, DP>(
mapStateToProps?: (state: Object) => SP,
mapDispatchToProps?: DP | (dispatch: Function) => DP,
mergeProps?: (stateProps: SP, dispatchProps: DP, props: P) => Object,
options?: {pure?: boolean, withRef?: boolean}
) => (component: Class<C>) => Class<React$Component<D, $Shape<P>, S>> This has two downsides:
This type only checks that the props you HAVE provided have the right type. (e. g. It won't let you pass a string instead of a number) But it will let you completely skip required props and not warn you. Do you think that is preferable? |
Update:
|
@nmn can you maybe give an example of the annotated connected component which is compatible with these type definitions? btw, you can probably use |
@nmn One more thing: you support |
|
I think its quite important to have some typesafey around state and dispatch. State isnt just any object, its a specific object you're expecting within the application. And dispatch isnt just a function, but a function that accepts specific actions. Perhaps connect could have a type signature like |
There are pretty good definitions available at reduxjs/redux#1887, for both redux and react-redux. |
thanks @agentcooper! those are some great lib defs! |
@agentcooper Those type definitions do look interesting. They are definitely more sound theoretically. But in the past, the most correct definitions have not worked for me in practice. I'll take these for a spin and report back, now that Flow has been updated a few times. |
Same as #538: Can this be rebased against the |
Has this been abandoned? |
Bump on this. Also opened up a relevant issue on flow-typed for the shorthand notation of |
This PR is on the back-burner as flow-typed already has the type-defs. There are some problems with function components that I'm trying to get fixed. |
@@ -0,0 +1,77 @@ | |||
/* @flow */ | |||
type ConnectAll = <D, P, S, C: React$Component<D, P, S>, SP, DP, Dispatch: Function>( | |||
mapStateToProps: (state: Object) => SP, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mapStateToProps can have additional parameter - own props of the connected component.
Closing for similar reasons to #814. Maintaining typings in-house is bad for DX. |
This adds a flow type declaration file so that anyone using it from NPM should just get the types 'for free'
It assumes the latest version of flow (0.25, though 0.24 should work too), and adds mostly adequate type definitions for both
connect
andProvider
. Obviously most of the code is there to typeconnect
which can get pretty tricky.Known Issues: Flow correctly doesn't support stateless React components. The type definition for connect doesn't change that fact. (I tried to do a trick to make such that any stateless function component decorated with
connect
would just start getting type checked, but that didn't quite work in practice.)