[英]how to fix the missing type in typescript
DATA = {
data: [
{ id: "1r5WioSdYLVqp", row: 1, assetType: "1r5WioSdYLVqp", status: "" },
{ id: "w1muKlmY4lsR", row: 2, assetType: "w1muKlmY4lsR", status: "" },
{ id: "1foDN2HDHiAbT", row: 3, assetType: "1foDN2HDHiAbT", status: "" }
]
};
new = { row: 1, assetType: "1fKBO4w0XHg7H", status: "ACTIVE" }
constructor() { }
ngOnInit() {
console.log(this.DATA);
this.DATA['data'].forEach((dt, index) => {
if (dt.row === this.new.row) {
this.DATA['data'][index] = this.new;
}
});
console.log(this.DATA)
}
What I'm trying to do here is to update the data based on the row number.我在这里要做的是根据行号更新数据。 but on the new it has no ID.
但在新的它没有ID。 but I'm getting an error which is:
但我收到一个错误,即:
We can indicate that an item my not have an id
by using a type annotation indicating that it is optional.我们可以通过使用指示它是可选的类型注释来指示一个项目可能没有
id
。
First, let's declare a type:首先,让我们声明一个类型:
interface Item {
id?: string;
row: number;
assetType: string;
status: string;
}
The ?
?
on id
above indicates that it is an optional property.上面的
id
表示它是一个可选属性。 It may or may not be specified.可以指定也可以不指定。
Now let's apply it:现在让我们应用它:
DATA: { data: Item[] } = {
data: [
{ id: "1r5WioSdYLVqp", row: 1, assetType: "1r5WioSdYLVqp", status: "" },
{ id: "w1muKlmY4lsR", row: 2, assetType: "w1muKlmY4lsR", status: "" },
{ id: "1foDN2HDHiAbT", row: 3, assetType: "1foDN2HDHiAbT", status: "" }
]
};
new = { row: 1, assetType: "1fKBO4w0XHg7H", status: "ACTIVE" }
constructor() { }
ngOnInit() {
console.log(this.DATA);
this.DATA.data.forEach((dt, index) => {
if (dt.row === this.new.row) {
this.DATA.data[index] = this.new; // no error.
}
});
console.log(this.DATA)
}
Notice, that I've snuck in an additional type, in the form of the object literal type { data: Item[] }
.请注意,我已经以 object 文字类型
{ data: Item[] }
的形式偷偷添加了一种类型。 This was done because of the nesting of the code in the question.这样做是因为问题中的代码嵌套。 If
DATA
has only one property, data
, I would unwrap it to simplify things.如果
DATA
只有一个属性data
,我会打开它以简化事情。
Then you could write然后你可以写
data: Item[] = [
{ id: "1r5WioSdYLVqp", row: 1, assetType: "1r5WioSdYLVqp", status: "" },
{ id: "w1muKlmY4lsR", row: 2, assetType: "w1muKlmY4lsR", status: "" },
{ id: "1foDN2HDHiAbT", row: 3, assetType: "1foDN2HDHiAbT", status: "" }
]
Which is easier to read and to write.哪个更容易阅读和写作。 Alternately, we can declare a type for the outer object
或者,我们可以为外部 object 声明一个类型
interface DataWrapper {
data: Item[];
}
DATA: DataWrapper = {
data: [
{ id: "1r5WioSdYLVqp", row: 1, assetType: "1r5WioSdYLVqp", status: "" },
{ id: "w1muKlmY4lsR", row: 2, assetType: "w1muKlmY4lsR", status: "" },
{ id: "1foDN2HDHiAbT", row: 3, assetType: "1foDN2HDHiAbT", status: "" }
]
}
Lastly, and unrelatedly, we can clean this code up a bit by removing the unecessary forEach
.最后,无关紧要,我们可以通过删除不必要的
forEach
来稍微清理一下这段代码。
const index = this.DATA.data.findIndex(({row}) => row === this.new.row);
if (index !== -1) {
this.DATA.data[index] = this.new;
}
set null on a property id
of the variable new
.在变量
new
的属性id
上设置 null 。
new = { id: null, row: 1, assetType: "1fKBO4w0XHg7H", status: "ACTIVE" }
this is probably a case where typescript is forcing you to write better code.这可能是 typescript 迫使您编写更好的代码的情况。 I can't think of any reason why you would want to generate data with a null id.
我想不出您为什么要使用 null id 生成数据的任何原因。 Why not something like this:
为什么不这样:
var id = generateId();
new = { id: id, row: 1, assetType: "1fKBO4w0XHg7H", status: "ACTIVE" };
I suggest to create a type for your object in data and set the type accordingly like below so it will help you hint what you're missing to avoid this kind of confusion:我建议在数据中为您的 object 创建一个类型,并相应地设置类型,如下所示,这样它将帮助您提示您缺少什么以避免这种混淆:
interface DataType {
id: string
row: number
assetType: string
status: string
}
DATA: Record<'data', DataType[]> = { data: [
{ id: "1r5WioSdYLVqp", row: 1, assetType: "1r5WioSdYLVqp", status: "" },
{ id: "w1muKlmY4lsR", row: 2, assetType: "w1muKlmY4lsR", status: "" },
{ id: "1foDN2HDHiAbT", row: 3, assetType: "1foDN2HDHiAbT", status: "" }
]}
new: DataType = {
id: 'yourNewId',
row: 1,
assetType: "1fKBO4w0XHg7H",
status: "ACTIVE"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.