英文:
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<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"],
},
],
},
];
To access a specific element in the array, I use the following code:
console.log("The name: ", mapDB[0]["places"][1]["members"][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 '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[]'
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: "HST",
places: [
{
place: "Oahu",
members: ["Frank", "Jerry", "Pearl"],
},
{
place: "Maui",
members: ["Susan", "Liana", "Bertha"],
},
],
},
{
timeZone: "PST",
places: [
{
place: "Tahiti",
members: ["Fido", "Snowy", "Butch"],
},
],
},
];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论