I have a component - 'App.tsx' that has a type definition for its props. The props include an array of todos and an action creator that will be injected by connect (react-redux). However I cannot wire the component 'App' in say 'index.tsx' without passing these required props. Is there an alternative to specifying these props as optional in the interface? If they are optional then I have to do null checks everywhere I access them.
I read through using redux with typescript among other documentation but found no answers.
index.tsx =>
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
The above code has errors because App component below expects the two params - 'todos' and 'fetchTodos'
App.tsx =>
interface AppProps {
todos: Todo[];
fetchTodos(): any;
}
class _App extends Component<AppProps> {
onButtonClick = (): void => {
this.props.fetchTodos();
}
renderList(): JSX.Element[] | undefined {
return this.props.todos.map((todo: Todo) => {
return (
<div key={todo.id}>{todo.title}</div>
);
});
}
render(): JSX.Element {
return (
<>
<button onClick={this.onButtonClick}>Fetch</button>
{this.renderList()}
</>
);
}
}
const mapStateToProps = ({ todos }: StoreState, ownProps: AppProps): { todos: Todo[]; } => {
return {
todos
};
};
export const App = connect(mapStateToProps, { fetchTodos })(_App);
I would like to see if there is a way to not specify the two props as optional in the AppProps interface because they would absolutely be injected by connect.