Skip to content

Add TypeScript typings corresponding to redux@next #812

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 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
295 changes: 295 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
import {
Component,
ComponentClass,
ComponentType,
ReactNode,
StatelessComponent,
} from 'react'

import {
Action,
Store,
Dispatch,
ActionCreator,
} from 'redux'

// Diff / Omit taken from https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-311923766
type Diff<T extends string, U extends string> = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T];
type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;

export interface DispatchProp {
dispatch?: Dispatch<{}>;
Copy link
Author

Choose a reason for hiding this comment

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

No need to have a type parameter at all here since we were only instantiating it with any. Hard-coding it to {} instead of any because it's in a contravariant position.

}

interface AdvancedComponentDecorator<TProps, TOwnProps> {
(component: ComponentType<TProps>): ComponentClass<TOwnProps>;
}

// Injects props and removes them from the prop requirements.
// Will not pass through the injected props if they are passed in during
// render. Also adds new prop requirements from TNeedsProps.
export interface InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps> {
<P extends TInjectedProps>(
component: ComponentType<P>
): ComponentClass<Omit<P, keyof TInjectedProps> & TNeedsProps> & {WrappedComponent: Component<P>}
}

// Injects props and removes them from the prop requirements.
// Will not pass through the injected props if they are passed in during
// render.
export type InferableComponentEnhancer<TInjectedProps> =
InferableComponentEnhancerWithProps<TInjectedProps, {}>

/**
* Connects a React component to a Redux store.
*
* - Without arguments, just wraps the component, without changing the behavior / props
*
* - If 2 params are passed (3rd param, mergeProps, is skipped), default behavior
* is to override ownProps (as stated in the docs), so what remains is everything that's
* not a state or dispatch prop
*
* - When 3rd param is passed, we don't know if ownProps propagate and whether they
* should be valid component props, because it depends on mergeProps implementation.
* As such, it is the user's responsibility to extend ownProps interface from state or
* dispatch props or both when applicable
*
* @param mapStateToProps
* @param mapDispatchToProps
* @param mergeProps
* @param options
*/
export interface Connect {
(): InferableComponentEnhancer<DispatchProp>;

<TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & DispatchProp, TOwnProps>;

<no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;

<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;

<TStateProps = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: null | undefined,
mergeProps: MergeProps<TStateProps, undefined, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;

<no_state = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<undefined, TDispatchProps, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;

<no_state = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: null | undefined,
mergeProps: MergeProps<undefined, undefined, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;

<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;

<TStateProps = {}, no_dispatch = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: null | undefined,
mergeProps: null | undefined,
options: Options<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<DispatchProp & TStateProps, TOwnProps>;

<no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<no_state, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;

<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;

<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps, TMergedProps>,
options: Options<TStateProps, TOwnProps, TMergedProps>
): InferableComponentEnhancerWithProps<TMergedProps, TOwnProps>;
}

/**
* The connect function. See {@type Connect} for details.
*/
export const connect: Connect;

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

interface MapStateToPropsFactory<TStateProps, TOwnProps> {
(initialState: any, ownProps: TOwnProps): MapStateToProps<TStateProps, TOwnProps>;
}

type MapStateToPropsParam<TStateProps, TOwnProps> = MapStateToPropsFactory<TStateProps, TOwnProps> | MapStateToProps<TStateProps, TOwnProps> | null | undefined;

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

type MapDispatchToProps<TDispatchProps, TOwnProps> =
MapDispatchToPropsFunction<TDispatchProps, TOwnProps> | TDispatchProps;

interface MapDispatchToPropsFactory<TDispatchProps, TOwnProps> {
(dispatch: Dispatch<any>, ownProps: TOwnProps): MapDispatchToProps<TDispatchProps, TOwnProps>;
}

type MapDispatchToPropsParam<TDispatchProps, TOwnProps> = MapDispatchToPropsFactory<TDispatchProps, TOwnProps> | MapDispatchToProps<TDispatchProps, TOwnProps>;

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

interface Options<TStateProps = {}, TOwnProps = {}, TMergedProps = {}> extends ConnectOptions {
/**
* If true, implements shouldComponentUpdate and shallowly compares the result of mergeProps,
* preventing unnecessary updates, assuming that the component is a “pure” component
* and does not rely on any input or state other than its props and the selected Redux store’s state.
* Defaults to true.
* @default true
*/
pure?: boolean;

/**
* When pure, compares incoming store state to its previous value.
* @default strictEqual
*/
areStatesEqual?: (nextState: any, prevState: any) => boolean;

/**
* When pure, compares incoming props to its previous value.
* @default shallowEqual
*/
areOwnPropsEqual?: (nextOwnProps: TOwnProps, prevOwnProps: TOwnProps) => boolean;

/**
* When pure, compares the result of mapStateToProps to its previous value.
* @default shallowEqual
*/
areStatePropsEqual?: (nextStateProps: TStateProps, prevStateProps: TStateProps) => boolean;

/**
* When pure, compares the result of mergeProps to its previous value.
* @default shallowEqual
*/
areMergedPropsEqual?: (nextMergedProps: TMergedProps, prevMergedProps: TMergedProps) => boolean;
}

/**
* Connects a React component to a Redux store. It is the base for {@link connect} but is less opinionated about
* how to combine <code>state</code>, <code>props</code>, and <code>dispatch</code> into your final props. It makes no
* assumptions about defaults or memoization of results, leaving those responsibilities to the caller.It does not
* modify the component class passed to it; instead, it returns a new, connected component class for you to use.
*
* @param selectorFactory The selector factory. See {@type SelectorFactory} for details.
* @param connectOptions If specified, further customizes the behavior of the connector. Additionally, any extra
* options will be passed through to your <code>selectorFactory</code> in the <code>factoryOptions</code> argument.
*/
export function connectAdvanced<S, TProps, TOwnProps, TFactoryOptions = {}>(
selectorFactory: SelectorFactory<S, TProps, TOwnProps, TFactoryOptions>,
connectOptions?: ConnectOptions & TFactoryOptions
): AdvancedComponentDecorator<TProps, TOwnProps>;

/**
* Initializes a selector function (during each instance's constructor). That selector function is called any time the
* connector component needs to compute new props, as a result of a store state change or receiving new props. The
* result of <code>selector</code> is expected to be a plain object, which is passed as the props to the wrapped
* component. If a consecutive call to <code>selector</code> returns the same object (<code>===</code>) as its previous
* call, the component will not be re-rendered. It's the responsibility of <code>selector</code> to return that
* previous object when appropriate.
*/
export interface SelectorFactory<S, TProps, TOwnProps, TFactoryOptions> {
(dispatch: Dispatch<S>, factoryOptions: TFactoryOptions): Selector<S, TProps, TOwnProps>
}

export interface Selector<S, TProps, TOwnProps> {
(state: S, ownProps: TOwnProps): TProps
}

export interface ConnectOptions {
/**
* Computes the connector component's displayName property relative to that of the wrapped component. Usually
* overridden by wrapper functions.
*
* @default name => 'ConnectAdvanced('+name+')'
* @param componentName
*/
getDisplayName?: (componentName: string) => string
/**
* Shown in error messages. Usually overridden by wrapper functions.
*
* @default 'connectAdvanced'
*/
methodName?: string
/**
* If defined, a property named this value will be added to the props passed to the wrapped component. Its value
* will be the number of times the component has been rendered, which can be useful for tracking down unnecessary
* re-renders.
*
* @default undefined
*/
renderCountProp?: string
/**
* Controls whether the connector component subscribes to redux store state changes. If set to false, it will only
* re-render on <code>componentWillReceiveProps</code>.
*
* @default true
*/
shouldHandleStateChanges?: boolean
/**
* The key of props/context to get the store. You probably only need this if you are in the inadvisable position of
* having multiple stores.
*
* @default 'store'
*/
storeKey?: string
/**
* If true, stores a ref to the wrapped component instance and makes it available via getWrappedInstance() method.
*
* @default false
*/
withRef?: boolean
}

export interface ProviderProps {
/**
* The single Redux store in your application.
*/
store?: Store<any, any>;
Copy link
Author

Choose a reason for hiding this comment

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

This is the main change from what's on DefinitelyTyped, which requires the new redux typings. Without it, users will probably run into problems instantiating Provider elements in the presence of TypeScript 2.6's --strictFunctionTypes.

children?: ReactNode;
}

/**
* Makes the Redux store available to the connect() calls in the component hierarchy below.
*/
export class Provider extends Component<ProviderProps> { }

/**
* Creates a new <Provider> which will set the Redux Store on the passed key of the context. You probably only need this
* if you are in the inadvisable position of having multiple stores. You will also need to pass the same storeKey to the
* options argument of connect.
*
* @param storeKey The key of the context on which to set the store.
*/
export function createProvider(storeKey: string): typeof Provider;
1 change: 1 addition & 0 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 Down