[英]Extending Type with new property isn't working
I'm trying to add one property to an existing Type.我正在尝试向现有类型添加一个属性。 Here's my type:
这是我的类型:
const dialogData: DialogData = {
a: string,
b: string,
c: string
}
I extend it like this:我这样扩展它:
export type ExtendedDialogData = DialogData & {
d: number
}
But when I try to create the object it's failing.但是当我尝试创建 object 时,它失败了。 My syntax must be completely wrong:
我的语法一定是完全错误的:
const myData: DialogData[{a:0, b:1, c:2}, {a:3, b:4, c:5}]
const myExtendedData: ExtendedDialogData[] = [...myData, d: 100 ];
The error is on d and says cannot find name 'd' .错误在d上,表示找不到名称 'd' 。 Also, in actuality, I just want d to be a copy of whatever c is.
另外,实际上,我只想d成为c的副本。 Can someone please help me understand my syntax error?
有人可以帮我理解我的语法错误吗?
UPDATE: As requested, here's the real code.更新:根据要求,这是真正的代码。 In a Material dialog in Angular I have these two types:
在 Angular 的材质对话框中,我有以下两种类型:
export type EditableDataModel = MyDataModel & {
sharesToMove: MyDataModel[];
}
export interface MyOrdersDialogData {
title: string,
orders: EditableDataModel[]
}
In the component that launches the model, I have this:在启动 model 的组件中,我有这个:
public launchModal() {
const orders: EditableDataModel[] = [...this.selectedOrders.map(el => {...el, sharesToMove: el.shares})[;
}
I can console.log inside the map and see the orders output like this:我可以在 map 中使用 console.log 并查看命令 output,如下所示:
{
title: 'my title',
orders: 1556
}
Here, you are trying to add d
directly to the array instead of adding to each object inside the array.在这里,您尝试将
d
直接添加到数组中,而不是添加到数组中的每个 object 中。 You can try like the following您可以尝试如下
const myData: DialogData[{a:0, b:1, c:2}, {a:3, b:4, c:5}]
const myExtendedData: ExtendedDialogData[] = [...myData.map(el => ({...el, d: 100})) ];
This will add the d property to each item in the array这会将 d 属性添加到数组中的每个项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.