Skip to content

Commit 3229d73

Browse files
committed
add typings
1 parent 217e98a commit 3229d73

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

index.d.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { ComponentClass, Component, StatelessComponent } from 'react';
2+
import { Store, Dispatch, ActionCreator } from 'redux';
3+
4+
interface ComponentDecorator<TOriginalProps, TOwnProps> {
5+
(component: StatelessComponent<TOriginalProps>|ComponentClass<TOriginalProps>): ComponentClass<TOwnProps>;
6+
}
7+
8+
/**
9+
* Following 5 functions cover all possible ways connect could be invoked
10+
*
11+
* - State: Redux state interface (the same one used by Store<S>)
12+
* - TOwnProps: Props passed to the wrapping component
13+
* - TStateProps: Result of MapStateToProps
14+
* - TDispatchProps: Result of MapDispatchToProps
15+
*/
16+
function connect<State, TOwnProps>(): ComponentDecorator<{ dispatch: Dispatch<State> } & TOwnProps, TOwnProps>;
17+
18+
function connect<State, TOwnProps, TStateProps>(
19+
mapStateToProps: FuncOrSelf<MapStateToProps<State, TOwnProps, TStateProps>>,
20+
): ComponentDecorator<TStateProps & { dispatch: Dispatch<State> } & TOwnProps, TOwnProps>;
21+
22+
function connect<State, TOwnProps, TStateProps, TDispatchProps>(
23+
mapStateToProps: FuncOrSelf<MapStateToProps<State, TOwnProps, TStateProps>>,
24+
mapDispatchToProps: FuncOrSelf<MapDispatchToPropsFunction<State, TOwnProps, TDispatchProps> | MapDispatchToPropsObject & TDispatchProps>
25+
): ComponentDecorator<TStateProps & TDispatchProps & TOwnProps, TOwnProps>;
26+
27+
function connect<State, TOwnProps, TDispatchProps>(
28+
mapStateToProps: null,
29+
mapDispatchToProps: FuncOrSelf<MapDispatchToPropsFunction<State, TOwnProps, TDispatchProps> | MapDispatchToPropsObject & TDispatchProps>
30+
): ComponentDecorator<TDispatchProps & TOwnProps, TOwnProps>;
31+
32+
function connect<State, TOwnProps, TStateProps, TDispatchProps, TMergeProps>(
33+
mapStateToProps: FuncOrSelf<MapStateToProps<State, TOwnProps, TStateProps>>,
34+
mapDispatchToProps: FuncOrSelf<MapDispatchToPropsFunction<State, TOwnProps, TDispatchProps>| MapDispatchToPropsObject & TDispatchProps>,
35+
mergeProps: MergeProps<TOwnProps, TStateProps, TDispatchProps, TMergeProps>,
36+
options?: Options
37+
): ComponentDecorator<TMergeProps, TOwnProps>;
38+
39+
interface MapDispatchToPropsObject {
40+
[name: string]: ActionCreator<any>;
41+
}
42+
43+
interface MapStateToProps<State, TOwnProps, TStateProps> {
44+
(state: State, ownProps: TOwnProps): TStateProps;
45+
}
46+
47+
interface MapDispatchToPropsFunction<State, TOwnProps, TDispatchProps> {
48+
(dispatch: Dispatch<State>, ownProps: TOwnProps): TDispatchProps;
49+
}
50+
51+
interface MergeProps<TOwnProps, TStateProps, TDispatchProps, TMergeProps> {
52+
(stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps): TMergeProps;
53+
}
54+
55+
interface Options {
56+
pure?: boolean;
57+
withRef?: boolean;
58+
}
59+
60+
type FuncOrSelf<T> = T | (() => T);
61+
62+
/**
63+
* Typescript does not support generic components in tsx yet in an intuïtive way which is the reason we avoid a
64+
* generic parameter in Store here by using any as the type
65+
*/
66+
export interface ProviderProps {
67+
store: Store<any>;
68+
}
69+
70+
export class Provider extends Component<ProviderProps, {}> { }

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "./lib/index.js",
66
"module": "es/index.js",
77
"jsnext:main": "es/index.js",
8+
"typings": "./index.d.ts",
89
"scripts": {
910
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
1011
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es",
@@ -27,7 +28,8 @@
2728
"dist",
2829
"lib",
2930
"src",
30-
"es"
31+
"es",
32+
"index.d.ts"
3133
],
3234
"keywords": [
3335
"react",

0 commit comments

Comments
 (0)