I am working with an object in Typescript that has a data
property structured like this:
type MyThing = {
data: {
options: {
myKey: string,
myValue: string
}[],
key: 'myKey',
value: 'myValue'
}
}
I want to create types of this structure using generics, but I am struggling to figure out the best approach. Here are the requirements:
- The base keys
key
andvalue
must be provided, but additional keys likedescription
could also be included. - The
options
should always be an array of objects containing only the values of the keys specified (e.g.,key
,value
, and potentiallydescription
).
While I can type this structure directly as shown above, I am unsure how to customize it with different keys or include extra options like descriptions.
An ideal solution would allow me to do something like this:
type MyThing = {
data: DataComposer<['key', 'value']>
// data: DataComposer<['key', 'value', 'description', ...]>
}