|
| 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, {}> { } |
0 commit comments