const obj1 = {
foo: 'bar',
goo: 'car',
hoo: 'dar',
ioo: 'far'
} as const
const obj2 = {
koo: 'gar',
loo: 'har',
moo: 'jar',
noo: 'kar'
} as const
type KeysOfObj1 = keyof typeof obj1 // "foo" | "goo" | "hoo" | "ioo"
type ValuesOfObj1 = typeof obj1[KeysOfObj1] // "bar" | "car" | "dar" | "far"
type KeysOfObj2 = keyof typeof obj2 // "koo" | "loo" | "moo" | "noo"
type ValuesOfObj2 = typeof obj2[KeysOfObj2] // "gar" | "har" | "jar" | "kar"
type OtherObj = Record<string, ValuesOfObj1 | ValuesOfObj2>
const obj3: OtherObj = {
hello: obj1.foo,
world: obj2.koo
} as const
const obj4: OtherObj = {
hi: obj1.hoo,
anotherWorld: obj2.moo
} as const
type KeysOfObj3 = keyof typeof obj3 // string
type KeysOfObj4 = keyof typeof obj4 // string
I believe the code is self-explanatory. My query pertains to obtaining keys of obj3 and obj4 as types in the format shown below, instead of just a generic "string" type:
type KeysOfObj3 = keyof typeof obj3 // "hello", "world"
type KeysOfObj4 = keyof typeof obj4 // "hi", "anotherWorld"
Please review the comments provided as they may offer additional insights.