Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit 171f9ff

Browse files
author
Huy Nguyen
committed
Manually add best type definitions for redux, react-redux, redux-thunk
TypeScript 2.0 introduces a better way of managing type definitions of third-party libraries using a combination of `npm` `@types` packages and type definitions built into each package. See microsoft/TypeScript#9184 for more details. As a result, I will migrate most of the type definitions currently managed by the `typings` tool over to these `npm` packages if the package authors didn't provide their own type definitions inside each package. That's the objective of this branch. However, although the author of `redux` does provide a type definition for his library (as an `index.d.ts` file inside the `redux` npm package), other `redux` plugins like `react-redux` and 'redux-thunk` has not updated their type definitions. As such, until those `redux` plugins update their type definitions, the type definitions for `redux` and those plugins will have to be "manually" managed i.e. not through an automated system like `tsd`, `typings` or `npm` packages. This commit adds the original version of these type definition files i.e. the versions that can be obtained through automated commands (listed below for reference). The next commit will slightly modify these definitions because they were intended to be used as "modular" definitions instead of as "global" definitions. - The `react-redux` type definition is version 4.4.8 of the `@types/react-redux` npm package. - The `redux-thunk` definition is version 2.0.27 of the `@types/redux-thunk` npm package. - The `redux` definition is the one that is bundled with version 3.5.2 of the `redux` npm package.
1 parent bed2dfd commit 171f9ff

File tree

3 files changed

+508
-0
lines changed

3 files changed

+508
-0
lines changed
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Type definitions for react-redux 4.4.0
2+
// Project: https://github.com/rackt/react-redux
3+
// Definitions by: Qubo <https://github.com/tkqubo>, Sean Kelley <https://github.com/seansfkelley>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
6+
/// <reference types="react" />
7+
/// <reference types="redux" />
8+
9+
import { ComponentClass, Component, StatelessComponent, ReactNode } from 'react';
10+
import { Store, Dispatch, ActionCreator } from 'redux';
11+
12+
interface ComponentDecorator<TOriginalProps, TOwnProps> {
13+
(component: ComponentClass<TOriginalProps> | StatelessComponent<TOriginalProps>): ComponentClass<TOwnProps>;
14+
}
15+
16+
/**
17+
* Decorator that infers the type from the original component
18+
*
19+
* Can't use the above decorator because it would default the type to {}
20+
*/
21+
export interface InferableComponentDecorator {
22+
<P, TComponentConstruct extends (ComponentClass<P> | StatelessComponent<P>)>(component: TComponentConstruct): TComponentConstruct;
23+
}
24+
25+
/**
26+
* Connects a React component to a Redux store.
27+
*
28+
* - Without arguments, just wraps the component, without changing the behavior / props
29+
*
30+
* - If 2 params are passed (3rd param, mergeProps, is skipped), default behavior
31+
* is to override ownProps (as stated in the docs), so what remains is everything that's
32+
* not a state or dispatch prop
33+
*
34+
* - When 3rd param is passed, we don't know if ownProps propagate and whether they
35+
* should be valid component props, because it depends on mergeProps implementation.
36+
* As such, it is the user's responsibility to extend ownProps interface from state or
37+
* dispatch props or both when applicable
38+
*
39+
* @param mapStateToProps
40+
* @param mapDispatchToProps
41+
* @param mergeProps
42+
* @param options
43+
*/
44+
export declare function connect(): InferableComponentDecorator;
45+
46+
export declare function connect<TStateProps, TDispatchProps, TOwnProps>(
47+
mapStateToProps: MapStateToProps<TStateProps, TOwnProps>,
48+
mapDispatchToProps?: MapDispatchToPropsFunction<TDispatchProps, TOwnProps> | MapDispatchToPropsObject
49+
): ComponentDecorator<TStateProps & TDispatchProps, TOwnProps>;
50+
51+
export declare function connect<TStateProps, TDispatchProps, TOwnProps>(
52+
mapStateToProps: MapStateToProps<TStateProps, TOwnProps>,
53+
mapDispatchToProps: MapDispatchToPropsFunction<TDispatchProps, TOwnProps> | MapDispatchToPropsObject,
54+
mergeProps: MergeProps<TStateProps, TDispatchProps, TOwnProps>,
55+
options?: Options
56+
): ComponentDecorator<TStateProps & TDispatchProps, TOwnProps>;
57+
58+
interface MapStateToProps<TStateProps, TOwnProps> {
59+
(state: any, ownProps?: TOwnProps): TStateProps;
60+
}
61+
62+
interface MapDispatchToPropsFunction<TDispatchProps, TOwnProps> {
63+
(dispatch: Dispatch, ownProps?: TOwnProps): TDispatchProps;
64+
}
65+
66+
interface MapDispatchToPropsObject {
67+
[name: string]: ActionCreator;
68+
}
69+
70+
interface MergeProps<TStateProps, TDispatchProps, TOwnProps> {
71+
(stateProps: TStateProps, dispatchProps: TDispatchProps, ownProps: TOwnProps): TStateProps & TDispatchProps;
72+
}
73+
74+
interface Options {
75+
/**
76+
* If true, implements shouldComponentUpdate and shallowly compares the result of mergeProps,
77+
* preventing unnecessary updates, assuming that the component is a “pure” component
78+
* and does not rely on any input or state other than its props and the selected Redux store’s state.
79+
* Defaults to true.
80+
* @default true
81+
*/
82+
pure: boolean;
83+
}
84+
85+
export interface ProviderProps {
86+
/**
87+
* The single Redux store in your application.
88+
*/
89+
store?: Store;
90+
children?: ReactNode;
91+
}
92+
93+
/**
94+
* Makes the Redux store available to the connect() calls in the component hierarchy below.
95+
*/
96+
export declare class Provider extends Component<ProviderProps, {}> { }
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Type definitions for redux-thunk v2.0.1
2+
// Project: https://github.com/gaearon/redux-thunk
3+
// Definitions by: Qubo <https://github.com/tkqubo>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
6+
/// <reference types="redux" />
7+
8+
import * as Redux from "redux";
9+
10+
declare var thunk: ReduxThunk.Thunk;
11+
export default thunk;
12+
export as namespace ReduxThunk;
13+
14+
declare namespace ReduxThunk {
15+
export interface Thunk extends Redux.Middleware {}
16+
export interface ThunkInterface {
17+
<T>(dispatch: Redux.Dispatch, getState?: () => T): any;
18+
}
19+
}

0 commit comments

Comments
 (0)