简体   繁体   English

如何修复 typescript 中缺少的类型

[英]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.

相关问题 如何修复 TypeScript function 类型错误? - How to fix TypeScript function type error? Typescript 类型检查错误如何在 React 中修复 - Typescript type checking error how to fix in React 缺少类型的属性(打字稿) - Missing properties for type (typescript) 如何在不添加迭代器的情况下修复打字稿中的迭代器缺失错误 - angular 8 - How to fix an iterator missing error in a typescript without adding an iterator - angular 8 如何在不使用关键字“any”的情况下修复 Typescript 中错位的类型 - How to fix misplaced type in Typescript without using keyword “any” 如何修复 Typescript 中的“元素”类型错误不存在属性? - How to fix property does not exist on type 'Element' error in Typescript? VSCode 如何修复 typescript 突出显示错误 {} 类型的参数不可分配给 - VSCode how to fix typescript highlight error Argument of type {} is not assignable to parameter of 尝试在 typescript 中键入 object 时如何解决问题? - How to fix problem when trying to type an object in typescript? 如何修复 typeScript 中的“对象”类型上不存在“属性”拨号 - how to fix 'Property 'dials' does not exist on type 'Object' in typeScript 如何修复'Typescript“正在进行类型检查......”花费太长时间'? - How to fix 'Typescript “Type checking in progress…” taking too long'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM