Skip to content

Add Typescript definitions. #433

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 1 commit into from
Closed
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
79 changes: 79 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@


declare module "react-redux" {
import { ComponentClass, Component, StatelessComponent, ReactNode } from 'react';
import { Store, Dispatch, ActionCreator } from 'redux';

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

/**
* Decorator that infers the type from the original component
*
* Can't use the above decorator because it would default the type to {}
*/
export interface InferableComponentDecorator {
<P, TComponentConstruct extends (ComponentClass<P>|StatelessComponent<P>)>(component: TComponentConstruct): TComponentConstruct;
}

/**
* Following 3 functions cover all possible ways connect could be invoked
*
* - State: Redux state interface (the same one used by Store<S>)
* - TStateProps: Result of MapStateToProps
* - TDispatchProps: Result of MapDispatchToProps
* - TOwnProps: Props passed to the wrapping component
*/
export function connect(): InferableComponentDecorator;

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

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

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

/**
* State is not actually used here but included for consistency with Redux typings and MapStateToProps.
*/
interface MapDispatchToPropsFunction<State, TDispatchProps, TOwnProps> {
(dispatch: Dispatch<State>, ownProps?: TOwnProps): TDispatchProps;
}

/**
* Any since not every ActionCreator returns the same Action
*/
interface MapDispatchToPropsObject {
[name: string]: ActionCreator<any>;
}

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

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

/**
* 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>;
children?: ReactNode;
}

export class Provider extends Component<ProviderProps, {}> { }
}