Skip to content

Add typings #538

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 16 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
4 changes: 0 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ npm run lint

Please open an issue with a proposal for a new feature or refactoring before starting on the work. We don't want you to waste your efforts on a pull request that we won't want to accept.

###Style

[reactjs](https://github.com/reactjs) is trying to keep a standard style across its various projects, which can be found over in [eslint-config-reactjs](https://github.com/reactjs/eslint-config-reactjs). If you have a style change proposal, it should first be proposed there. If accepted, we will be happy to accept a PR to implement it here.

## Submitting Changes

* Open a new issue in the [Issue tracker](https://github.com/reactjs/react-redux/issues).
Expand Down
70 changes: 70 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ComponentClass, Component, StatelessComponent } from 'react';
import { Store, Dispatch, ActionCreator } from 'redux';

interface ComponentDecorator<TOriginalProps, TOwnProps> {
(component: StatelessComponent<TOriginalProps>|ComponentClass<TOriginalProps>): ComponentClass<TOwnProps>;
}

/**
* Following 5 functions cover all possible ways connect could be invoked
*
* - State: Redux state interface (the same one used by Store<S>)
* - TOwnProps: Props passed to the wrapping component
* - TStateProps: Result of MapStateToProps
* - TDispatchProps: Result of MapDispatchToProps
*/
function connect<State, TOwnProps>(): ComponentDecorator<{ dispatch: Dispatch<State> } & TOwnProps, TOwnProps>;

function connect<State, TOwnProps, TStateProps>(
mapStateToProps: FuncOrSelf<MapStateToProps<State, TOwnProps, TStateProps>>,
): ComponentDecorator<TStateProps & { dispatch: Dispatch<State> } & TOwnProps, TOwnProps>;

function connect<State, TOwnProps, TStateProps, TDispatchProps>(
mapStateToProps: FuncOrSelf<MapStateToProps<State, TOwnProps, TStateProps>>,
mapDispatchToProps: FuncOrSelf<MapDispatchToPropsFunction<State, TOwnProps, TDispatchProps> | MapDispatchToPropsObject & TDispatchProps>
Copy link

Choose a reason for hiding this comment

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

In a case of passing object to mapDispatchToProps, what is TDispatchProps? What's the difference with MapDispatchToPropsObject?

I think it should be a different signature for the case when mapDispatchToProps is object, something like this:

function connect<State, TOwnProps, TStateProps, TDispatchProps extends MapDispatchToPropsObject>(
  mapStateToProps: FuncOrSelf<MapStateToProps<State, TOwnProps, TStateProps>>,
  mapDispatchToProps: TDispatchProps
): ComponentDecorator<TStateProps & TDispatchProps & TOwnProps, TOwnProps>;

Copy link
Author

Choose a reason for hiding this comment

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

TDispatchProps may be set by the user. It's the list of dispatchable action creators he intends to use in the component.
MapDispatchToPropsObject is a constraint we add to make sure values of the hash are action creators.

Adding a different signature is problematic because the combination will be very large. Same issue as with mapStateToProps: null.

): ComponentDecorator<TStateProps & TDispatchProps & TOwnProps, TOwnProps>;

function connect<State, TOwnProps, TDispatchProps>(
mapStateToProps: null,
mapDispatchToProps: FuncOrSelf<MapDispatchToPropsFunction<State, TOwnProps, TDispatchProps> | MapDispatchToPropsObject & TDispatchProps>
): ComponentDecorator<TDispatchProps & TOwnProps, TOwnProps>;

function connect<State, TOwnProps, TStateProps, TDispatchProps, TMergeProps>(
mapStateToProps: FuncOrSelf<MapStateToProps<State, TOwnProps, TStateProps>>,
mapDispatchToProps: FuncOrSelf<MapDispatchToPropsFunction<State, TOwnProps, TDispatchProps>| MapDispatchToPropsObject & TDispatchProps>,
mergeProps: MergeProps<TOwnProps, TStateProps, TDispatchProps, TMergeProps>,
options?: Options
): ComponentDecorator<TMergeProps, TOwnProps>;

interface MapDispatchToPropsObject {
[name: string]: ActionCreator<any>;
}

interface MapStateToProps<State, TOwnProps, TStateProps> {
(state: State, ownProps: TOwnProps): TStateProps;
}

interface MapDispatchToPropsFunction<State, TOwnProps, TDispatchProps> {
(dispatch: Dispatch<State>, ownProps: TOwnProps): TDispatchProps;
}

interface MergeProps<TOwnProps, TStateProps, TDispatchProps, TMergeProps> {
(stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps): TMergeProps;
}

interface Options {
pure?: boolean;
withRef?: boolean;
}

type FuncOrSelf<T> = T | (() => T);

/**
* Typescript does not support generic components in tsx yet in an intuïtive way which is the reason we avoid a
* generic parameter in Store here by using any as the type
*/
export interface ProviderProps {
store: Store<any>;
}

export class Provider extends Component<ProviderProps, {}> { }
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "4.4.5",
"description": "Official React bindings for Redux",
"main": "./lib/index.js",
"typings": "./index.d.ts",
"scripts": {
"build:lib": "babel src --out-dir lib",
"build:umd": "cross-env NODE_ENV=development webpack src/index.js dist/react-redux.js",
Expand All @@ -22,7 +23,8 @@
"files": [
"dist",
"lib",
"src"
"src",
"index.d.ts"
],
"keywords": [
"react",
Expand Down