I have a nested object by keys that looks like the following:
const OBJECTS = {
OBJECT1: {
properties: {
prop1: { value: "a" },
},
},
OBJECT2: {
properties: {
prop2: { value: "b" },
},
},
} as const;
I'm trying to create a generic type where I an extract out the property values for a specific object - something like:
type PropertiesForObject<OBJECT_KEY extends keyof typeof OBJECTS> = {
[PROPERTY_NAME in keyof typeof OBJECTS[OBJECT_KEY]["properties"]]: typeof OBJECTS[OBJECT_KEY]["properties"][PROPERTY_NAME]["value"];
};
However, I'm getting the following error:
Type '"value"' cannot be used to index type '{ readonly OBJECT1: { readonly properties: { readonly prop1: { readonly value: "a"; }; }; }; readonly OBJECT2: { readonly properties: { readonly prop2: { readonly value: "b"; }; }; }; }[OBJECT_KEY]["properties"][PROPERTY_NAME]'.
Is there any way to index into each object's properties, given that the shape of the properties is the same? I could force it into an interface, but I want to keep the strong typings of the values.
PropertiesForObject<'OBJECT1'>to yield{ prop1: "a" }?