(TS)点差类型只能从对象类型创建

时间:2019-04-21 01:17:45

标签: typescript

我一直在执行Aurelia-DragNDrop。但是,它是用Javascript编写的文档和示例,而我正在使用Typescript。

在实现示例的过程中,我遇到了以上错误。因此,我在SO上研究了散布算子,并发现了this。 它建议使用“ ...(作为对象的xxx)”,但是根据我的收集,我的变量已经被键入为数组。其他答案不清楚或以React为导向-我什至没有尝试过Typescript游乐场。

代码:

@computedFrom('items', 'intention')
get patchedItems() {
    const { items, intention } = this;
    if (!intention) return items;

    let patched = _.reject(items, { id: intention.id });
    const item = _.find(this.items, { id: intention.id });

    if (item) {
        // always show current moving item on top
        patched.push({ ...item, x: intention.x, y: intention.y });
    }

    return patched;
}

项目已在类顶部声明为:

@bindable items = [
    { id: 'a', name: 'A', x: 20, y: 20 },
    { id: 'b', name: 'B', x: 50, y: 200 },
    { id: 'c', name: 'C', x: 200, y: 100 }
];

还有人在谈论这将在3个版本中解决。我使用的是Typescript 3.4。

这里是错误的图片:

Spread Types can only be created from object types

我该怎么写,以便编译器不会抱怨?

1 个答案:

答案 0 :(得分:0)

我认为问题不是传播运算符,而是您使用的underscore方法错误,因此出现了意外情况。对于reject,您必须传递一个函数而不是一个对象,像这样:

const patched = _.reject(items, (i) => i.id === intention.id)

然后对find进行相同操作,也可以改用findWhere

const item = _.find(this.items, (i) => i.id === intention.id)
// or
const item = _.findWhere(items, { id: itention.id })