I struggled with this for the longest time. Today he sat down to get rid of anyone anyon the left, but failed.
import * as React from 'react';
import * as Redux from 'redux';
import { connect } from 'react-redux';
import { ReduxState } from './types';
import { syncItem, displayAlert } from './actionCreators';
import { SyncItemAction, DisplayAlertAction } from './actions';
type AppData = {
isSelected: boolean;
isInEditMode: boolean;
};
type AppActions = {
syncItem: (id: number) => SyncItemAction;
displayAlert: (text: string) => DisplayAlertAction;
};
type AppProps = {
id: number;
name: string;
} & Partial<AppData> & Partial<AppActions>;
type AppState = Partial<{
temp: string;
}>;
@connect<AppData, AppActions, AppProps>(mapStateToProps, mapDispatchToProps)(App)
export default class App extends React.Component<AppProps, AppState> {
constructor(props: AppProps) {
super(props);
}
render() {
return (
<div>
<h1>Hello, {this.props.name}! (
{}
Rename: <input value={this.state.temp} onChange={event => this.setState({ temp: event.target.value })} />
<button onClick={_ => this.props.syncItem(this.props.id)}>Sync</button>
</div>
);
}
}
function mapStateToProps(state: ReduxState, ownProps?: AppProps): AppData {
return {
isSelected: ownProps.id === state.selectedId,
isInEditMode: state.isInEditMode
};
}
function mapDispatchToProps(dispatch: Redux.Dispatch<ReduxState>, ownProps?: AppProps): AppActions {
return {
syncItem: (id: number) => dispatch(syncItem(id)),
displayAlert: (text: string) => dispatch(displayAlert(text, ownProps.name))
};
}
function mapDispatchToPropsAlt(dispatch: Redux.Dispatch<ReduxState>, ownProps?: AppProps): AppActions {
return {
syncItem,
displayAlert: null
};
}
function Test() {
return <App id={0} name={'test'} />;
}
In the above code, I get the following:
index.tsx(31,78): error TS2345: Argument of type 'typeof App' is not assignable to parameter of type 'ComponentClass<AppData & AppActions> | StatelessComponent<AppData & AppActions>'.
Type 'typeof App' is not assignable to type 'StatelessComponent<AppData & AppActions>'.
Type 'typeof App' provides no match for the signature '(props: AppData & AppActions & { children?: ReactNode; }, context?: any): ReactElement<any>'
I used type definitions in node_modules/@typesto come up with what I had above, and in a similar way I checked what it looks like ComponentClass<T>. He suggests (it seems to me) that there stateshould be a component {} | void, which I don’t understand why this is so, and also if I changed the code above to say <AppProps, void>or <AppProps, {}>, it doesn’t change the first error anyway.
How should I do it?
syncItemis simple function syncItem(id: number): SyncItemActionand SyncItemActionis interface SyncItemAction extends Redux.Action { id: number; }, likewise displayAlert.
: , , state.