user2024080
user2024080

Reputation: 5101

Type has no matching index signature for type 'string'.(2537)

my code for reference:

const MyArray = [
  { name: "Alice", age: 15 },
  { name: "Bob", age: 23 },
  { name: "Eve", age: 38 },
];

type Name = typeof MyArray[string]["name"]; //throws error as Type '{ name: string; age: number; }[]' has no matching index signature for type 'string'
type Age = typeof MyArray[number]["age"] //no error

I am trying to access the name as like age. then why I am getting this error. any one help me to understand please?

Thanks in advacne

Upvotes: 0

Views: 1077

Answers (1)

nullptr
nullptr

Reputation: 4494

If you want to access the name like you're accessing age, you can do typeof MyArray[number]["name"]. This is because the array indices are numbers.

Upvotes: 1

Related Questions