如何在Typescript中打印自定义对象的属性?

艾伦·斯维茨(Alan Svits)

我想prop: valuemyObject[stringProp]语法返回一个自定义对象的数组我得到这样的错误:

     TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{} | EmployeeListDataRow'.
  No index signature with a parameter of type 'string' was found on type '{} | EmployeeListDataRow'.
  <h1>
    {Object.keys(currentEmployee).map((prop) => {
      return `${prop}: ${currentEmployee[prop]}\n`;
    })}
  </h1>

currentEmployee 具有这样的界面:

export interface EmployeeListDataRow extends Object {
  id: number;
  name: string;
  startDate: Date;
  status: string;
  manager: string;
  title: string;
  account: string;
  team: string;
  rate: number;
  totalComp: number;
  gm: number;
  ebit: number;
  billable: string;
  phone: string;
  skills: string;
}
圣索纳瓦拉

这是因为您没有明确告诉TypeScriptEmployeeListDataRow可以由字符串索引。您需要修改接口以告诉TypeScript您的接口是可字符串索引的:

export interface EmployeeListDataRow {  // no need to extend Object
  [key: string]: number | string | Date;   // this is an index signature
  id: number;
  name: string;
  startDate: Date;
  status: string;
  manager: string;
  title: string;
  account: string;
  team: string;
  rate: number;
  totalComp: number;
  gm: number;
  ebit: number;
  billable: string;
  phone: string;
  skills: string;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在python中打印自定义对象的数组?

如何打印QML对象的自定义属性列表?

如何在clojurescript中为本机对象自定义打印功能

Powershell-如何在自定义对象的属性中添加多个对象?

如何在Worklight中的用户身份对象中设置和检索自定义属性?

如何在Racket中自定义程序的打印?

如何在TypeScript中从地图的自定义键类型获取对象?

如何在自定义排序功能中对对象的特定属性实施排序?

如何在java中检索特定视图对象的自定义属性?

如何在PowerShell中向自定义对象动态添加新属性

如何在jQuery中存在自定义属性的多个条件下选择对象?

如何在Drupal 8的Drupal用户对象中访问自定义字段属性

如何从React中的事件对象访问自定义属性?

如何在TypeScript中迭代自定义文字类型?

如何在Typescript中创建自定义事件?

如何在cloudformation中添加自定义属性?

如何在Quasar中添加自定义属性?

如何在 html 中自定义 onmouseover 属性?

如何在Kylo中添加自定义类别属性?

如何在自定义指令中获取评估的属性

如何在JSX中添加自定义html属性

如何在自定义组件中评估FacesComponent属性

如何在JAXB中自定义属性名称?

如何在ProdctData JSON中填充自定义属性?

如何在C#中创建自定义属性

如何在Azure中自定义用户属性的类型?

如何在自定义WebViewPage中设置属性?

如何在自定义HttpMessageHandler中设置属性?

如何在Javers中自定义属性名称