[react-redux] implemented proper typings for thunk action creators in TDispatchProps

This commit is contained in:
Boris Sergeyev
2018-11-03 19:20:07 +01:00
parent 882b31e47f
commit f482096dc5
2 changed files with 55 additions and 6 deletions
+32 -6
View File
@@ -1,4 +1,4 @@
// Type definitions for react-redux 6.0.4
// Type definitions for react-redux 6.0.5
// Project: https://github.com/reduxjs/react-redux
// Definitions by: Qubo <https://github.com/tkqubo>,
// Kenzie Togami <https://github.com/kenzierocks>,
@@ -11,6 +11,7 @@
// Valentin Descamps <https://github.com/val1984>
// Johann Rakotoharisoa <https://github.com/jrakotoharisoa>
// Anatoli Papirovski <https://github.com/apapirovski>
// Boris Sergeyev <https://github.com/surgeboris>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
@@ -102,7 +103,7 @@ type ConnectedComponentClass<C, P> = ComponentClass<JSX.LibraryManagedAttributes
// 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> {
<C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>>(
<C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>>(
component: C
): ConnectedComponentClass<C, Omit<GetProps<C>, keyof Shared<TInjectedProps, GetProps<C>>> & TNeedsProps>
}
@@ -113,6 +114,19 @@ export interface InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps
export type InferableComponentEnhancer<TInjectedProps> =
InferableComponentEnhancerWithProps<TInjectedProps, {}>
// redux-thunk middleware returns thunk's return value from dispatch call
type WithThunkActionCreators<TDispatchProps> =
TDispatchProps extends object
? {
[C in keyof TDispatchProps]: C extends (...args: any[]) => any
? ReturnType<C> extends (...args: any[]) => any
? ReturnType<ReturnType<C>>
: C
: never
}
: TDispatchProps
/**
* Connects a React component to a Redux store.
*
@@ -142,12 +156,18 @@ export interface Connect {
<no_state = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps: null | undefined,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
): InferableComponentEnhancerWithProps<
WithThunkActionCreators<TDispatchProps>,
TOwnProps
>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
): InferableComponentEnhancerWithProps<
TStateProps & WithThunkActionCreators<TDispatchProps>,
TOwnProps
>;
<TStateProps = {}, no_dispatch = {}, TOwnProps = {}, TMergedProps = {}, State = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
@@ -185,14 +205,20 @@ export interface Connect {
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<{}, TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TDispatchProps, TOwnProps>;
): InferableComponentEnhancerWithProps<
WithThunkActionCreators<TDispatchProps>,
TOwnProps
>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, State = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
mergeProps: null | undefined,
options: Options<State, TStateProps, TOwnProps>
): InferableComponentEnhancerWithProps<TStateProps & TDispatchProps, TOwnProps>;
): InferableComponentEnhancerWithProps<
TStateProps & WithThunkActionCreators<TDispatchProps>,
TOwnProps
>;
<TStateProps = {}, TDispatchProps = {}, TOwnProps = {}, TMergedProps = {}, State = {}>(
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
+23
View File
@@ -99,6 +99,29 @@ namespace MapDispatch {
const verifyUndefined = <TestUndefined foo='bar' />
}
namespace MapDispatchWithThunkActionCreators {
interface OwnProps { foo: string }
interface DispatchProps {
onClick(): void,
thunkAction(): Promise<void>
}
class TestComponent extends Component<OwnProps & DispatchProps> {}
const mapDispatchToProps = () => ({
onClick: () => {},
thunkAction: () => async () => {}
})
const Test = connect(
null,
mapDispatchToProps,
)(TestComponent)
const verify = <Test foo='bar' />
}
namespace MapStateAndDispatchObject {
interface ClickPayload { count: number }
const onClick: ActionCreator<ClickPayload> = () => ({ count: 1 });