Allow me to illustrate my query with an example rather than simply using words:
interface Y {
x: number,
y: string
}
interface PairValue<T> {
key: keyof T,
value: T[this['key']]
}
const pairValue: PairValue<Y> = {
key: 'x',
value: 'Issue! This should be a number'
};
My objective is to have the type of value
determined by the value of key
. In other words, if key
is x, then I want the type of value
to match the type of Y[x]
, which is number
. However, at present, the type of value
is string | number
, resulting from the union of all value types in Y
.
Is it possible to accomplish this using the current features available in TypeScript?