Мне наконец-то удалось получить правильно набранный подключенный компонент, в основном используя его в качестве отправной точки https://github.com/reduxjs/redux-thunk/blob/master/test/typescript.ts для правильной типизации асинхронных действий создателя действий.Ключ заключается в том, чтобы правильно ввести ваш магазин и использовать ThunkDispatch и ThunkAction из redux-thunk.
Store.tsx
import { createStore, applyMiddleware, combineReducers } from "redux";
import { playground, IState } from "./reducer";
import thunk, { ThunkMiddleware } from "redux-thunk";
import { IActions } from "./actions";
export interface RootState {
playground: IState;
}
export type RootActions = IActions;
const rootReducer = combineReducers({ playground });
const store = createStore<RootState, RootActions, {}, {}>(
rootReducer,
applyMiddleware(thunk as ThunkMiddleware<RootState, RootActions>)
Actions.tsx
import { Action } from "redux";
import { ThunkAction } from "redux-thunk";
import { RootState, RootActions } from "./store";
type ThunkResult<R> = ThunkAction<R, RootState, undefined, RootActions>;
//Actions
export enum TestActionTypes {
THUNK_ACTION = "THUNK_ACTION",
}
export interface AsyncAction extends Action {
type: TestActionTypes.THUNK_ACTION;
}
export type IActions = AsyncAction;
//Action creator
export function testAsyncActionCreator(): ThunkResult<void> {
return (dispatch, getState) => {
console.log(getState);
dispatch({ type: TestActionTypes.THUNK_ACTION });
};
}
Reducer.tsx
import { IActions, TestActionTypes } from "./actions";
import { Reducer } from "redux";
export interface IState {}
const defaultValue: IState = {};
export const playground: Reducer<IState, IActions> = (
state = defaultValue,
action
) => {
switch (action.type) {
default:
return state;
}
};
ConnectedComponent.tsx
...
import { connect } from "react-redux";
import { RootState, RootActions } from "./redux/store";
import { syncActionCreator, testGetState } from "./redux/actions";
import { ThunkDispatch } from "redux-thunk";
interface OwnProps {}
interface StateProps {}
interface DispatchProps {
test: () => void;
}
type Props = OwnProps & StateProps & DispatchProps;
class DumbContainer extends React.Component<Props> {
render() {
return (
...
);
}
}
const mapStateToProps = (state: RootState): StateProps => {
return {
text: state.playground.text
};
};
const mapDispatchToProps = (
dispatch: ThunkDispatch<RootState, undefined, RootActions>
): DispatchProps => {
return {
test: () => {
dispatch(testAsyncActionCreator());
}
};
};
export default connect<StateProps, DispatchProps, OwnProps, RootState>(
mapStateToProps,
mapDispatchToProps
)(DumbContainer);
Я такжеесть проект детской площадки здесь https://github.com/vbvxx/playground-reactnative-typescript