Skip to content

Commit a0f2388

Browse files
Got rid of any type
1 parent 8ec9a4e commit a0f2388

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

lib/connect.tsx

+33-28
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
1-
import { Store } from "./store";
1+
import { Store, Dispatch } from "./store";
22
import * as React from "react";
33
import { Subscription } from "rxjs";
44
import { Reducers } from "./reducer";
5-
import { PayloadOfActionCreator } from "./action";
5+
import { ActionCreator } from "./action";
66

77
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
8-
9-
type MapActionProps<TActionProps> = { [K in keyof TActionProps]: (payload: PayloadOfActionCreator<TActionProps[K]>) => void };
10-
118
type SelectorProps<TState> = { [key: string]: (state: TState) => unknown };
9+
type ActionProps = { [key: string]: ActionCreator<string, unknown> };
1210

13-
const mapSelectorProps = <TState, TStateProps extends SelectorProps<TState>>(state: TState, stateProps: TStateProps) => {
14-
const props: any = {};
15-
16-
for (const key in stateProps) {
17-
const selector = stateProps[key];
18-
props[key] = selector(state);
19-
}
20-
21-
return props;
11+
const mapSelectorProps = <TState, TSelectorProps extends SelectorProps<TState>, TProps, TSelectorKeys extends keyof TProps>(
12+
state: TState,
13+
selectorProps: TSelectorProps,
14+
) => {
15+
const stateProps = Object.keys(selectorProps).reduce(
16+
(stateProps, key) => {
17+
stateProps[key] = selectorProps[key](state);
18+
return stateProps;
19+
},
20+
{} as Pick<TProps, TSelectorKeys>,
21+
);
22+
return stateProps;
2223
};
2324

24-
const mapActionProps = <TState, TReducers, TActionProps>(store: Store<TState, TReducers>, actionProps: TActionProps) => {
25-
const dispatchProps: any = {};
26-
27-
for (const key in actionProps) {
28-
dispatchProps[key] = (payload) => {
29-
const action = (actionProps[key] as any)(payload);
30-
store.dispatch(action as any);
31-
};
32-
}
25+
const mapActionProps = <TState, TReducers, TActionProps, TProps, TActionKeys extends keyof TProps>(
26+
store: Store<TState, TReducers>,
27+
actionProps: TActionProps,
28+
) => {
29+
const dispatchProps = Object.keys(actionProps).reduce(
30+
(dispatchProps, key) => {
31+
dispatchProps[key] = (payload) => store.dispatch(actionProps[key](payload));
32+
return dispatchProps;
33+
},
34+
{} as Pick<TProps, TActionKeys>,
35+
);
3336

3437
return dispatchProps;
3538
};
3639

3740
export const connect = <TState, TReducers extends Reducers<TState>>(store: Store<TState, TReducers>) => <
3841
TSelectorProps extends SelectorProps<TState>,
39-
TActionProps
42+
TActionProps extends ActionProps
4043
>(
4144
stateProps: TSelectorProps,
4245
actionProps: TActionProps,
4346
) => <
44-
TProps extends Pick<TProps, keyof TSelectorProps> & MapActionProps<TActionProps>,
47+
TProps extends Pick<TProps, keyof TSelectorProps> & Pick<TProps, keyof TActionProps>,
4548
ExternalProps = Omit<TProps, keyof TSelectorProps | keyof TActionProps>
4649
>(
4750
Component: React.ComponentType<TProps>,
4851
): React.ComponentType<ExternalProps> => {
49-
const dispatchProps = mapActionProps(store, actionProps);
52+
const dispatchProps = mapActionProps<TState, TReducers, TActionProps, TProps, keyof TActionProps>(store, actionProps);
5053

5154
return class extends React.Component<ExternalProps, { subscription: Subscription; stateProps: Pick<TProps, keyof TSelectorProps> }> {
5255
public constructor(props: ExternalProps) {
@@ -56,7 +59,7 @@ export const connect = <TState, TReducers extends Reducers<TState>>(store: Store
5659
public componentDidMount(): void {
5760
store.state$.subscribe((state) => {
5861
this.setState({
59-
stateProps: mapSelectorProps(state, stateProps),
62+
stateProps: mapSelectorProps<TState, TSelectorProps, TProps, keyof TSelectorProps>(state, stateProps),
6063
});
6164
});
6265
}
@@ -68,7 +71,9 @@ export const connect = <TState, TReducers extends Reducers<TState>>(store: Store
6871
}
6972

7073
public render(): JSX.Element {
71-
const WrappedComponent = (Component as unknown) as React.ComponentType<ExternalProps & Pick<TProps, keyof TSelectorProps> & MapActionProps<TActionProps>>;
74+
const WrappedComponent = (Component as unknown) as React.ComponentType<
75+
ExternalProps & Pick<TProps, keyof TSelectorProps> & Pick<TProps, keyof TActionProps>
76+
>;
7277
return this.state && <WrappedComponent {...this.props} {...this.state.stateProps} {...dispatchProps} />;
7378
}
7479
};

store/hello-world/action.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export enum HelloWorldActionType {
44
SayHello = "SAY_HELLO",
55
}
66

7-
export const sayHelloAction: ActionCreator<HelloWorldActionType.SayHello> = () => ({
7+
export const sayHelloAction: ActionCreator<HelloWorldActionType.SayHello, string> = (payload) => ({
88
type: HelloWorldActionType.SayHello,
9+
payload,
910
});

0 commit comments

Comments
 (0)