Skip to content

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[ignore]
<PROJECT_ROOT>/node_modules/fbjs/.*

[include]

[libs]

[options]
module.use_strict=true
munge_underscores=true
suppress_comment= \\(.\\|\n\\)*\\$FlowDisableLine
suppress_comment= \\(.\\|\n\\)*\\$FlowBug
esproposal.class_static_fields=enable
esproposal.class_instance_fields=enable
esproposal.decorators=ignore
traces=3

[version]
>=0.25.0
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ node_modules
npm-debug.log
.DS_Store
dist
lib
lib/*
!lib/index.js.flow
coverage
77 changes: 77 additions & 0 deletions lib/index.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* @flow */
type ConnectAll = <D, P, S, C: React$Component<D, P, S>, SP, DP, Dispatch: Function>(
mapStateToProps: (state: Object) => SP,
Copy link

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.

mapDispatchToProps: (dispatch: Dispatch) => DP,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An important case that we want to support is when mapDispatchToProps is an object with action creators ({ addTodo, removeTodo }) rather than a function. Then injected props will be functions with the same names ({ addTodo, removeTodo }) but those functions would dispatch automatically.

Copy link
Author

@nmn nmn May 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Thankfully that's an easy thing to account for. Will fix.
Can't fix.

mergeProps: null | void,
options?: {pure?: boolean, withRef?: boolean}
) => (component: Class<C>) => Class<React$Component<D, $Diff<$Diff<P, DP>, SP>, S>>

type ConnectAllStateless = <P, SP, DP, Dispatch: Function>(
mapStateToProps: (state: Object) => SP,
mapDispatchToProps: (dispatch: Dispatch) => DP,
mergeProps: null | void,
options?: {pure?: boolean, withRef?: boolean}
) => (component: (props: P) => any) => Class<React$Component<void, $Diff<$Diff<P, DP>, SP>, void>>

type ConnectMerged = <D, P, S, C: React$Component<D, P, S>, SP, DP, MP, Dispatch: Function>(
mapStateToProps: (state: Object) => SP,
mapDispatchToProps: (dispatch: Dispatch) => DP,
mergeProps: (stateProps: SP, dispatchProps: DP, origProps: P) => MP,
options?: {pure?: boolean, withRef?: boolean}
) => (component: Class<C>) => Class<React$Component<D, $Diff<P, MP>, S>>

type ConnectMergedStateless = <P, SP, DP, MP, Dispatch: Function>(
mapStateToProps: (state: Object) => SP,
mapDispatchToProps: (dispatch: Dispatch) => DP,
mergeProps: (stateProps: SP, dispatchProps: DP, origProps: P) => MP,
options?: {pure?: boolean, withRef?: boolean}
) => (component: (props: P) => any) => Class<React$Component<void, $Diff<P, MP>, void>>

type ConnectNoState = <D, P, S, C: React$Component<D, P, S>, DP, Dispatch: Function>(
mapStateToProps: null | void,
mapDispatchToProps: (dispatch: Dispatch) => DP,
mergeProps: null | void,
options?: {pure?: boolean, withRef?: boolean}
) => (component: Class<C>) => Class<React$Component<D, $Diff<P, DP>, S>>

type ConnectNoStateStatless = <P, DP, Dispatch: Function>(
mapStateToProps: null | void,
mapDispatchToProps: (dispatch: Dispatch) => DP,
mergeProps: null | void,
options?: {pure?: boolean, withRef?: boolean}
) => (component: (props: P) => any) => Class<React$Component<void, $Diff<P, DP>, void>>

type ConnectDispatch = <D, P, S, C: React$Component<D, P, S>, SP, Dispatch: Function>(
mapStateToProps: (state: Object) => SP,
mapDispatchToProps: null | void,
mergeProps: null | void,
options?: {pure?: boolean, withRef?: boolean}
) => (component: Class<C>) => Class<React$Component<D, $Diff<$Diff<P, {dispatch: Dispatch}>, SP>, S>>

type ConnectDispatchStateless = <P, SP, Dispatch: Function>(
mapStateToProps: (state: Object) => SP,
mapDispatchToProps: null | void,
mergeProps: null | void,
options?: {pure?: boolean, withRef?: boolean}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think options are always forced to be the last argument. (I wanted to make passing them inconvenient because they enable escape hatches like withRef and pure: false.) So you probably don’t need so many overloads.

Only connect(fn, null, null, options) would work, not connect(fn, options).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't know that. Still the overloads are still needed because every combination changes the type of the returned value. Every overload in the file has a different return type! I'll change the type defs though.

) => (component: (props: P) => any) => Class<React$Component<void, $Diff<$Diff<P, {dispatch: Dispatch}>, SP>, void>>

type ConnectDefault = <D, P, S, C: React$Component<D, P, S>, Dispatch: Function>() =>
(component: Class<C>) => Class<React$Component<D, $Diff<P, {dispatch: Dispatch}>, S>>

type ConnectDefaultStateless = () =>
<P>(component: (props: P) => any) => Class<React$Component<void, $Diff<P, {dispatch: Function}>, void>>

declare export var connect
: ConnectAll
& ConnectAllStateless
& ConnectMerged
& ConnectMergedStateless
& ConnectNoState
& ConnectNoStateStatless
& ConnectDispatch
& ConnectDispatchStateless
& ConnectDefault
& ConnectDefaultStateless;
declare export var Provider: ReactClass<{store: Object, children?: any}>;

export {connect, Provider}