I want to create an interface with generic keys limited by, say an enum, ensuring that the interface only accept certain keys and values.
I imagine doing something like this
interface IFruit<TargetKey extends string, TargetValue> {
[key: TargetKey]: TargetValue;
}
Where the interface could be defined using the following:
enum Keys {
APPLES = 'APPLES',
ORANGES = 'ORANGES',
}
enum Values {
GREEN = 'GREEN',
BLUE = 'BLUE',
ORANGE = 'ORANGE',
}
const test: IFruit<Keys, Values> = {
Keys.APPLES: Values.GREEN,
'POTATOES': 'BLUE', // Should give an error, as key is not in Keys enum
}
I'm not sure if this is possible to even achieve. So far I get an index signature error An index signature parameter type must be 'string' or 'number'
. Is there a way to get this to work?