I have a data property inside of an object I'm looking to type in Typescript. It looks like the following:
type MyThing = {
data: {
options: {
myKey: string,
myValue: string
}[],
key: 'myKey',
value: 'myValue'
}
}
I would like to be able to instantiate types of this structure using a generic but can't quite wrap my mind around what I should be doing. Here are the requirements:
keyandvalueare the base "keys" that must be supplied, but there may be others:descriptionfor instanceoptionsshould always be an array of objects containing only the values of thekey,value, and possiblydescriptiondefined below
I have no problems typing this literally (like in the above example). But if I want to supply custom keys and values ("myKey" and "myValue" respectively), or if I want to specify other options like a description beyond the key and value, I cannot figure out how to do such.
The desired solution would be for me to do something like this:
type MyThing = {
data: DataComposer<['key', 'value']>
// data: DataComposer<['key', 'value', 'description', ...]>
}
"myKey"). Does this meet your needs? tsplay.dev/mb0r2wkeyandvalue. Those values will end up being the properties of theoptionsarray of objects, but injecting their values is not necessary... and in fact, the values will not be present at this location in the code. I would simply like to say "define the string for the key, define the string for the value, and use those to form the object inside of the options array".descriptionor how they should be treated. Maybe you can add an example showing those?options? tsplay.dev/wXK08Woptionsthey must also be present as a property with a string value outside ofoptions. Remember, the values of the "outside properties" are the KEYS of theoptionsarray of objects.