If I have a typescript type consisting of keys:
const anObject = {value1: '1', value2: '2', value3: '3'}
type objectKeys = keyof typeof anObject
and then I wish to add keys to that type, while retaining the current keys, how do I go about doing that?
for example, if I wanted to add the keys 'get_value1', 'get_value2', 'get_value3' to the type 'objectKeys'
In the end, I want a type that looks like so:
type objectKeys = keyof anObject + 'get_value1', 'get_value2', 'get_value3'
without having to manually define the keys prefixed with 'get_', I understand that I can type out the keys to create this object - however that is not feasible for my use case. I simply want to add some keys that may or may not exist to the type 'objectKeys'
I am also aware that I can create a generic or any type that allows for any key value, however I must know the actual key names. It does not help me to allow for ANY key to be requested of the object, I need the existing keys + the ones I'd like to add.
Thanks for any help.
added 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
})
// now I don't know whats next, how do I update objectKeys to include the
// additions added in the forEach loop.
// What I really want is to not have to add the 'get' values to the object
// at all, JUST to the type. I want typechecking for the get values that
// may or may not actually exist on the object.
hope thats clearerer and such.