首页 > 解决方案 > TypeScript 中缺少属性

问题描述

class RolloutStoreClass {

import { observable, action, makeAutoObservable } from "mobx";


    public queue = observable<IDeploymentProject>([]);
    public inProcess = observable<IDeploymentProject>([]);
    public successfull = observable<IDeploymentProject>([]);
    public failed = observable<IDeploymentProject>([]);


constructor() {
        makeAutoObservable(this);
    }


@action
    private clearQueue(): void {
        this.queue = [] ;
        this.inProcess = [];
        this.failed = [];
        this.successfull = [];
    }
}

export const RolloutStore = new RolloutStoreClass();

我在 clearQueue 函数上得到了这个问题。队列

错误是:

在“never []”类型中,缺少“Observable Array”类型的以下属性:“spliceWithArray, clear, replace, remove, toJSON”。

标签: reactjstypescriptmobx

解决方案


您要么需要使您的queue(和其他字段)常规数组,所有可观察性仍然有效。

或者使用.clear()里面的方法clearQueue

    private clearQueue(): void {
        this.queue.clear();
        // ...
    }

还有一件事:当你使用时,makeAutoObservable你不需要显式标记action'sobservable等等,你可以删除所有的装饰器:

class Foo {
    // Don't need explicit observable decorator
    public queue: IDeploymentProject[] = [];
    // ...

    constructor() {
        makeAutoObservable(this);
    }
    
    // You can remove action decorator
    private clearQueue(): void {
        this.queue = [] ;
        this.inProcess = [];
        this.failed = [];
        this.successfull = [];
    }
}

推荐阅读