I have a complex nested object structure defined by keys, which can be illustrated as follows:
const DATA = {
INFO1: {
details: {
detail1: { value: "x" },
},
},
INFO2: {
details: {
detail2: { value: "y" },
},
},
} as const;
My goal is to devise a generic type that allows me to extract the values of specific properties within a given object - for example:
type DetailsForObject<INFO_KEY extends keyof typeof DATA> = {
[DETAIL_NAME in keyof typeof DATA[INFO_KEY]["details"]]: typeof DATA[INFO_KEY]["details"][DETAIL_NAME]["value"];
};
However, I am encountering an error stating:
Type '"value"' cannot be used to index type
'{ readonly INFO1: { readonly details: { readonly detail1: { readonly value: "x"; }; }; };
readonly INFO2: { readonly details: { readonly detail2: { readonly value: "y"; }; }; }; }[INFO_KEY]["details"][DETAIL_NAME]'.
Is it possible to access each object's properties using indexing, considering that they share identical property shapes? Although I could coerce it into an interface, I prefer to maintain strict typing for the values.