Many times, the keys of a record determine its value. For instance:
const record = {
[2]: 5,
["string"]: "otherString",
["there is"]: "a pattern"
}
In these instances, each key of type K corresponds to the value K.
Here's a possible syntax for this concept (although not valid Typescript):
type DynamicIndex = {
[key<T>: T]: T
}
So this would be valid:
const record: DynamicIndex = {
[2]: 5,
["string"]: "otherString",
["there is"]: "a pattern"
}
But this wouldn't work:
const record: DynamicIndex = {
[2]: "string",
["string"]: "otherString",
["there is"]: "a pattern"
}
Is there a way to achieve similar functionality? In my case, the value is Something<T>
and not exactly T
:
type ComplexCase<V> = {
[key<K extends typeof V>: K]: Something<V[K]>
}