【发布时间】:2019-01-06 13:10:16
【问题描述】:
在我的 Angular 项目中,我有一个包含类型和状态字段的对象。状态可能因类型而异。有人可能会说这是一个糟糕的设计,但目前就是这样。关键是我似乎不能在这个对象上使用扩展运算符,因为编译器说Property 'getStatusList' is missing in type ...
我简化了这个类,但它看起来像这样:
export class Foo {
constructor(
public type: number,
public status: number
) { }
getStatusList(): string[] {
switch (this.type) {
case 3: {
return ['Off', 'Interval', 'On Hold']
}
default: {
return ['Off', 'On']
}
}
}
}
这是 typescript 2.9.2 和 angular cli 6.1.1
下面的代码会触发错误,但是在 typescript playground 中编写示例代码时,我想也许我应该使用 Object.assign。虽然我仍然认为我可以简单地使用扩展运算符否决对象的某些属性?
const oldFoo: Foo = new Foo(3, 5);
console.log('old status: ', oldFoo.status);
const newFoo: Foo = { ...oldFoo, type: 1, status: 7 };
console.log('new status: ', newFoo.status);
【问题讨论】:
-
那里没有展开运算符 ...
-
请同时包含触发错误的代码。
标签: angular typescript typescript2.0