Imagine having an interface with just one key and value :
interface X {
Y : string
}
It would be great to define a key-value type like this:
interface Z {
"key" : Y,
"value" : string
}
However, manually doing this can be tedious. What if we could write a generic type or interface to automate this process, based on a single type argument?
For example:
interface KeyValueGenerator<U> {
...
}
so that Z
above is essentially KeyValueGenerator<X>
What should be included in ...
to make it function correctly?
My attempt so far has been:
interface KeyValueGenerator<U> {
key : (keyof U)[0],
value : U[(keyof U)[0]]
}
However, there is an issue with using a string to index U.
When I try to resolve this, it seems to default to the any type for the value:
interface kv2<U extends {[key :string] : U[(keyof U)[0]]}> {
key : keyof U,
value : U[(keyof U)[0]]
}
Any suggestions or alternatives are welcome.