I need to convert because I am currently using the following code to get the hints (keys) of object A.
const ObjectA = { "Something" : "To show", "Hello", : "World" }
const GetObjectKey: { [key in keyof typeof ObjectA]: string } = ObjectA;
Then I will get those keys to do another task. But there is a problem, when I get the value of GetObjectKey[MyKey] it returns the value of that key. I just want it to return the key only.
What do I need to do to return the key of that object and still keep autocomplete. Thanks
CodePudding user response:
const ObjectA = { "Something": "To show", "Hello": "World" }
const GetObjectKey: { [key in keyof typeof ObjectA]: key } = Object.keys(ObjectA).reduce<any>((pv, cv) => (pv[cv] = cv, pv), {})
var hello = GetObjectKey['Hello']
Build a new object with key to key in runtime.