Typescript (TS)排列类型只能从对象类型创建
我一直在实施Aurelia DragNDrop。然而,它是用Javascript编写的,文档和示例也是如此,我正在使用Typescript 在实现这些示例的过程中,我遇到了上述错误。因此,我在SO上研究了spread运算符并发现。 它建议使用“…(xxx作为对象)”,但是我的变量已经是我可以收集的数组类型。其他答案不清楚,也不以反应为导向——我甚至尝试了打字游戏,但都没有成功 守则:Typescript (TS)排列类型只能从对象类型创建,typescript,Typescript,我一直在实施Aurelia DragNDrop。然而,它是用Javascript编写的,文档和示例也是如此,我正在使用Typescript 在实现这些示例的过程中,我遇到了上述错误。因此,我在SO上研究了spread运算符并发现。 它建议使用“…(xxx作为对象)”,但是我的变量已经是我可以收集的数组类型。其他答案不清楚,也不以反应为导向——我甚至尝试了打字游戏,但都没有成功 守则: @computedFrom('items', 'intention') get patchedItems() {
@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 }
];
也有传言说,这将在第三章中被提及。。我使用的是Typescript 3.4
下面是错误的图片:
我如何编写它,使编译器不会抱怨?我认为问题不在于扩展运算符,而在于您使用了
下划线的方法,因此得到了意外的结果。例如,必须传递函数而不是对象,如下所示:
const patched = _.reject(items, (i) => i.id === intention.id)
然后对find
执行相同的操作,或者您可以使用:
我认为问题不在于spread运算符,而在于您使用了下划线
的方法错误,因此得到了意想不到的结果。例如,必须传递函数而不是对象,如下所示:
const patched = _.reject(items, (i) => i.id === intention.id)
然后对find
执行相同的操作,或者您可以使用: