小编Ivá*_*QMX的帖子

使用 TypeScript 推送到状态数组 React

我正在尝试通过 addTask 方法在我的任务数组中添加一个任务对象,该方法获取先前的状态并添加新任务,它可以工作,JSX但在我使用 TypeScript 时不起作用。我收到的错误是

Type 'ITask[] | undefined' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.  TS2569
Run Code Online (Sandbox Code Playgroud)
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 => ({ …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

reactjs ×1

typescript ×1