Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading