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

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

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

问题

  1. 我有一个存储在多维关联数组中的人员数组。每个人属于一个地方,每个地方属于一个时区。我设置了我的变量如下:
  2. ```typescript
  3. interface AssociativeArray {
  4. [key: string]: Array<object> | string | number | object;
  5. }
  6. export var mapDB: AssociativeArray[] = [
  7. {
  8. timeZone: "HST",
  9. places: [
  10. {
  11. place: "Oahu",
  12. members: ["Frank", "Jerry", "Pearl"],
  13. },
  14. {
  15. place: "Maui",
  16. members: ["Susan", "Liana", "Bertha"],
  17. },
  18. ],
  19. },
  20. {
  21. timeZone: "PST",
  22. places: [
  23. {
  24. place: "Tahiti",
  25. members: ["Fido", "Snowy", "Butch"],
  26. },
  27. ],
  28. },
  29. ];

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

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

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

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. ```typescript
  5. interface AssociativeArray {
  6. [key: string]: Array&lt;object&gt; | string | number | object;
  7. }
  8. export var mapDB: AssociativeArray[] = [
  9. {
  10. timeZone: &quot;HST&quot;,
  11. places: [
  12. {
  13. place: &quot;Oahu&quot;,
  14. members: [&quot;Frank&quot;, &quot;Jerry&quot;, &quot;Pearl&quot;],
  15. },
  16. {
  17. place: &quot;Maui&quot;,
  18. members: [&quot;Susan&quot;, &quot;Liana&quot;, &quot;Bertha&quot;],
  19. },
  20. ],
  21. },
  22. {
  23. timeZone: &quot;PST&quot;,
  24. places: [
  25. {
  26. place: &quot;Tahiti&quot;,
  27. members: [&quot;Fido&quot;, &quot;Snowy&quot;, &quot;Butch&quot;],
  28. },
  29. ],
  30. },
  31. ];

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

  1. 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:

  1. 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;.
  2. 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 无法推断多维关联数组中嵌套元素的实际类型。您需要对类型更加具体:

  1. interface Place {
  2. place: string;
  3. members: string[];
  4. }
  5. interface TimeZone {
  6. timeZone: string;
  7. places: Place[];
  8. }
  9. export const mapDB: TimeZone[] = [
  10. {
  11. timeZone: "HST",
  12. places: [
  13. {
  14. place: "Oahu",
  15. members: ["Frank", "Jerry", "Pearl"],
  16. },
  17. {
  18. place: "Maui",
  19. members: ["Susan", "Liana", "Bertha"],
  20. },
  21. ],
  22. },
  23. {
  24. timeZone: "PST",
  25. places: [
  26. {
  27. place: "Tahiti",
  28. members: ["Fido", "Snowy", "Butch"],
  29. },
  30. ],
  31. },
  32. ];
英文:

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:

  1. interface Place {
  2. place: string;
  3. members: string[];
  4. }
  5. interface TimeZone {
  6. timeZone: string;
  7. places: Place[];
  8. }
  9. export const mapDB: TimeZone[] = [
  10. {
  11. timeZone: &quot;HST&quot;,
  12. places: [
  13. {
  14. place: &quot;Oahu&quot;,
  15. members: [&quot;Frank&quot;, &quot;Jerry&quot;, &quot;Pearl&quot;],
  16. },
  17. {
  18. place: &quot;Maui&quot;,
  19. members: [&quot;Susan&quot;, &quot;Liana&quot;, &quot;Bertha&quot;],
  20. },
  21. ],
  22. },
  23. {
  24. timeZone: &quot;PST&quot;,
  25. places: [
  26. {
  27. place: &quot;Tahiti&quot;,
  28. members: [&quot;Fido&quot;, &quot;Snowy&quot;, &quot;Butch&quot;],
  29. },
  30. ],
  31. },
  32. ];

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:

确定