If I have a typescript type with keys:
const anObject = {value1: '1', value2: '2', value3: '3'}
type objectKeys = keyof typeof anObject
and I want to add additional keys to the type without manually defining them, how can I achieve this?
For instance, if I intend to include the keys 'get_value1', 'get_value2', 'get_value3' to the existing type 'objectKeys'
Ultimately, I aim for a type structure like this:
type objectKeys = keyof anObject + 'get_value1', 'get_value2', 'get_value3'
Instead of individually specifying keys prefixed with 'get_', I seek a method to append keys dynamically to the 'objectKeys' type. This is necessary for my specific scenario as typing out all the keys manually is not practical.
I understand that I could create a generic or any type that allows for any key value, but that doesn't serve my purpose. I require the existing keys along with the new ones I wish to include in 'objectKeys'.
Thank you for any assistance provided.
Additional information for clarity:
const anObject = {val1: '1', val2: '2'}
type objectKeys = keyof typeof anObject
Object.keys(anObject).forEach(key => {
const getAddition = `get_${key}`
anObject[getAddition] = getAddition
})
// After adding new keys using forEach loop, how do I update objectKeys
// to reflect these additions?
// My main goal is to update the 'objectKeys' type without altering the
// keys in the object itself. I want typechecking for the new 'get' values
// that may or may not exist in the object.
I hope this clarification helps.