Skip to content

Add TypeScript typings (revisited) #815

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 8 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ before_install:
script:
- npm run lint
- npm run test:cov
- npm run test:typescript
after_success:
- npm run coverage
67 changes: 67 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ComponentClass, Component, StatelessComponent } from 'react';
import { Store, Dispatch, ActionCreator, Action } 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
*/
export function connect<State, TOwnProps>(): ComponentDecorator<{ dispatch: Dispatch<State> } & TOwnProps, TOwnProps>;

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

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

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

interface MapDispatchToPropsObject {
[name: string]: ActionCreator<Action> | ThunkedActionCreator;
}

type ThunkedActionCreator = (...args: any[]) => (dispatch: Dispatch<any>) => void

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, {}> { }
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "./lib/index.js",
"module": "es/index.js",
"jsnext:main": "es/index.js",
"typings": "./index.d.ts",
"scripts": {
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es",
Expand All @@ -17,6 +18,7 @@
"test": "cross-env BABEL_ENV=commonjs NODE_ENV=test mocha --compilers js:babel-register --recursive --require ./test/setup.js",
"test:watch": "npm test -- --watch",
"test:cov": "cross-env NODE_ENV=test nyc npm test",
"test:typescript": "typings-tester --dir test/typescript",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
},
"repository": {
Expand All @@ -27,7 +29,8 @@
"dist",
"lib",
"src",
"es"
"es",
"index.d.ts"
],
"keywords": [
"react",
Expand All @@ -47,6 +50,7 @@
},
"homepage": "https://github.com/gaearon/react-redux",
"devDependencies": {
"@types/react": "^16.0.18",
"babel-cli": "^6.3.17",
"babel-core": "^6.3.26",
"babel-eslint": "^7.1.1",
Expand Down Expand Up @@ -98,7 +102,9 @@
"rollup-plugin-commonjs": "^8.0.2",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-replace": "^1.1.1",
"rollup-plugin-uglify": "^2.0.1"
"rollup-plugin-uglify": "^2.0.1",
"typescript": "^2.0.10",
"typings-tester": "^0.2.0"
},
"dependencies": {
"hoist-non-react-statics": "^2.2.1",
Expand Down
Loading