I'm trying to add a task object in my tasks array by the addTask method, this method its getting the previous state and adding the new task, it works with JSX but not when I am using TypeScript. The error that I'm getting is
Type 'ITask[] | undefined' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators. TS2569
import React, { Fragment } from 'react';
interface ITask {
name: string;
done: boolean;
}
interface IProps {
}
interface IState {
newTask?: string;
tasks?: Array<ITask>;
}
class App extends React.Component<IProps, IState>{
constructor(props: any) {
super(props);
this.state = {
newTask: '',
tasks: []
}
}
addTask(name:string) {
this.setState(prevState => ({
tasks: [...prevState.tasks, {name, done: false}]
}));
}
render() {
return (
<Fragment>
</Fragment>
);
}
}