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
CodePudding user response:
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.