我收到有关一个正常工作的TypeScript多维数组的警告的原因是什么?

huangapple go评论73阅读模式
英文:

Why am I getting warnings about a TypeScript multidimensional array that works?

问题

我有一个存储在多维关联数组中的人员数组。每个人属于一个地方,每个地方属于一个时区。我设置了我的变量如下:

```typescript
interface AssociativeArray {
    [key: string]: Array<object> | string | number | object;
}

export var mapDB: AssociativeArray[] = [
    {
        timeZone: "HST",
        places: [
            {
                place: "Oahu",
                members: ["Frank", "Jerry", "Pearl"],
            },
            {
                place: "Maui",
                members: ["Susan", "Liana", "Bertha"],
            },
        ],
    },
    {
        timeZone: "PST",
        places: [
            {
                place: "Tahiti",
                members: ["Fido", "Snowy", "Butch"],
            },
        ],
    },
];

要访问数组中的特定元素,我使用以下代码:

console.log("The name: ", mapDB[0]["places"][1]["members"][2]);

这段代码返回了预期值 "Bertha"。然而,在 VS Code 中,这一行标有错误消息:

Element implicitly has an 'any' type because expression of type '1' can't be used to index type 'string | number | object | object[]'.
  Property '1' does not exist on type 'string | number | object | object[]'

我已经尝试编写不同版本的 "interface",但似乎无法消除错误消息。有人能帮我修复吗?


<details>
<summary>英文:</summary>

I have an array of people stored in a multidimensional associative array. Each person belongs to a place and each place belongs to a timezone. I set up my variable like this:

```typescript
interface AssociativeArray {
    [key: string]: Array&lt;object&gt; | string | number | object;
}

export var mapDB: AssociativeArray[] = [
    {
        timeZone: &quot;HST&quot;,
        places: [
            {
                place: &quot;Oahu&quot;,
                members: [&quot;Frank&quot;, &quot;Jerry&quot;, &quot;Pearl&quot;],
            },
            {
                place: &quot;Maui&quot;,
                members: [&quot;Susan&quot;, &quot;Liana&quot;, &quot;Bertha&quot;],
            },
        ],
    },
    {
        timeZone: &quot;PST&quot;,
        places: [
            {
                place: &quot;Tahiti&quot;,
                members: [&quot;Fido&quot;, &quot;Snowy&quot;, &quot;Butch&quot;],
            },
        ],
    },
];

To access a specific element in the array, I use the following code:

console.log(&quot;The name: &quot;, mapDB[0][&quot;places&quot;][1][&quot;members&quot;][2]);

This code returns the expected value of "Bertha". However, in VS Code, this line is marked with an error message:

Element implicitly has an &#39;any&#39; type because expression of type &#39;1&#39; can&#39;t be used to index type &#39;string | number | object | object[]&#39;.
  Property &#39;1&#39; does not exist on type &#39;string | number | object | object[]&#39;

I have tried to write different versions of the "interface" but I cannot seem to get rid of the error message. Can someone help me fix it?

答案1

得分: 2

错误发生是因为 TypeScript 无法推断多维关联数组中嵌套元素的实际类型。您需要对类型更加具体:

interface Place {
  place: string;
  members: string[];
}

interface TimeZone {
  timeZone: string;
  places: Place[];
}

export const mapDB: TimeZone[] = [
  {
    timeZone: "HST",
    places: [
      {
        place: "Oahu",
        members: ["Frank", "Jerry", "Pearl"],
      },
      {
        place: "Maui",
        members: ["Susan", "Liana", "Bertha"],
      },
    ],
  },
  {
    timeZone: "PST",
    places: [
      {
        place: "Tahiti",
        members: ["Fido", "Snowy", "Butch"],
      },
    ],
  },
];
英文:

The error happens because TypeScript cannot infer the actual types of the nested elements in the multidimensional associative array. You need to be more specific with the types:

interface Place {
  place: string;
  members: string[];
}

interface TimeZone {
  timeZone: string;
  places: Place[];
}

export const mapDB: TimeZone[] = [
  {
    timeZone: &quot;HST&quot;,
    places: [
      {
        place: &quot;Oahu&quot;,
        members: [&quot;Frank&quot;, &quot;Jerry&quot;, &quot;Pearl&quot;],
      },
      {
        place: &quot;Maui&quot;,
        members: [&quot;Susan&quot;, &quot;Liana&quot;, &quot;Bertha&quot;],
      },
    ],
  },
  {
    timeZone: &quot;PST&quot;,
    places: [
      {
        place: &quot;Tahiti&quot;,
        members: [&quot;Fido&quot;, &quot;Snowy&quot;, &quot;Butch&quot;],
      },
    ],
  },
];

huangapple
  • 本文由 发表于 2023年6月16日 13:28:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定