I have objects that implement types as shown below:
type TMyObject<T extends string> {
category: T
}
I am trying to store them statically in another object while ensuring that the key of this second object matches the value of the category
field, like this:
const myObject: TMyObject<'foo'> = { category: 'foo' }
const dictionary = {
foo: myObject, // correct
bar: myObject, // not valid: key 'bar' does not match myObject's category
}
This issue arises because I have interfaces that extend IMyObject
and set the category field to a specific value, as demonstrated:
type TMyFooObject = IMyObject<'foo'>
I have spent a significant amount of time attempting to create a type for the dictionary
object that functions as described, but I am struggling to find a solution ^^
It is important to note that the category
field and the potential types extending TMyObject
are dynamic, so a simple union cannot be used here...
Thank you for taking the time to read and potentially respond to this inquiry!