如何在typescript中访问类型化对象值

如何在typescript中访问类型化对象值,typescript,Typescript,我在谷歌上搜索了好几个小时,但对这个问题我还是没办法想清楚 我有一个界面: interface Alert { uuid: string, city: string, address: number } const alerts: Alert[] = response.from.api.alerts interface Alert extends Indexable { uuid: string, city: string, address: number } 我

我在谷歌上搜索了好几个小时,但对这个问题我还是没办法想清楚

我有一个界面:

interface Alert {
  uuid: string,
  city: string,
  address: number
}
 const alerts: Alert[] = response.from.api.alerts
interface Alert extends Indexable {
  uuid: string,
  city: string,
  address: number
}
我想对一个对象进行测试,该对象肯定符合此接口:

interface Alert {
  uuid: string,
  city: string,
  address: number
}
 const alerts: Alert[] = response.from.api.alerts
interface Alert extends Indexable {
  uuid: string,
  city: string,
  address: number
}
现在,当我尝试执行类似操作时,ts编译器中出现一个错误:

for (const alert of alerts) {
 for(const field in alert) {
   // this shows an error: Element implicitly has an 'any' type... ts(7053)
   console.log(alert[field])
 }
}
获取警报对象值的正确方法是什么

我应该使用类型防护装置或类似装置吗

如何向typescript声明该字段是一个keyof alert

我是否应该禁用noImplicitAny标志->这是否被视为“最佳实践”

我知道这个问题已经被问过很多次了,但没有一个答案对我有帮助


谢谢你的帮助

你可以试试这样的东西

    alerts.forEach( (field: Alert) => {
      console.log(field.uuid);
    });

在我看来,如果您有接口,并且您确信对象符合您的接口,那么您不应该迭代对象的每个键。Underwood,typescript会相信你并假设
警报
是一个
警报数组

所以基本上你可以这样做:

const alertsWithoutType: any = [
    { uuid: "uuid1", city: "city1", address: 1},
    { uuid: "uuid2", city: "city2", address: 2}
];

const alerts = alertsWithoutType as Array<Alert>;

alerts.forEach(alert => console.log(alert));
export interface Indexable {
  [key: string]: any;
}

因此,我找到了一种处理此错误的方法(ts.7053)

首先,谢谢大家的帮助,但我相信我的问题有误导性。正确的问题应该是->如何在for循环中使用括号符号访问类型化对象

我创建了一个名为Indexable的新接口,如下所示:

const alertsWithoutType: any = [
    { uuid: "uuid1", city: "city1", address: 1},
    { uuid: "uuid2", city: "city2", address: 2}
];

const alerts = alertsWithoutType as Array<Alert>;

alerts.forEach(alert => console.log(alert));
export interface Indexable {
  [key: string]: any;
}
我的“警报”界面现在正在扩展此界面:

interface Alert {
  uuid: string,
  city: string,
  address: number
}
 const alerts: Alert[] = response.from.api.alerts
interface Alert extends Indexable {
  uuid: string,
  city: string,
  address: number
}
现在,我可以对键入为“警报”的对象执行for循环,而不会出现错误:

...
 for(const field in alert) {
   console.log(alert[field]
 }
...

接口在编译时是未知的,其中可能有任何内容。如果你看,你可以在里面放任何东西。可能会有帮助吗?请注意,
alert
是一个块作用域函数。我已经回顾了这些答案,但没有一个与手头的问题相关。第一个答案不是使用接口,第二个答案是尝试从接口获取对象键,这很好,但不是我的问题。我很好奇如何“动态”访问typescript对象中的属性。访问alert[field]是我想在typescript世界中学习的技巧。一定有办法做到这一点。@MoxxiManagarm你能详细说明一下吗?@Stutje还有,我的错误不在编译时。我可以用//@ts ignore通过它,但这不是目标。我想以某种“最佳实践”的方式来实现这一点,我在访问direct字段(即alert.uuid)时没有问题。也许我问的问题并不准确。我想在for-in循环中进行迭代,并动态访问已键入的对象的属性。在您的解决方案中,最让我头疼的是使用“any”关键字。如果我知道响应的类型,为什么要使用“any”?我不精通typescript,但我认为这是typescript中的一种反模式,我尝试将其作为最后手段使用。@JaniskoPsisko您应该避免使用
any
。如果知道类型,则应键入变量。对于您的情况,如果您知道接口,为什么要迭代对象的每个属性类型?可以使用正确的类型名访问每个属性。这就是typescript检索错误的原因。可能有很多原因。我在这个问题中提供的示例并不重要,但在我正在处理的现实世界场景中,我想强制将该项目设置为我需要的适当形状。i、 e对象中的所有字符串都应大写。这样做是合法的,
if(typeof alert[field]=='string')让capitalizedVal=alert[field]。toUpperCase()
。。。