Is it possible to restrict the type of a certain key to a certain value in TypeScript for an object?
For instance, consider the following example:
interface ValueType1 {
key1: string,
key2: string,
}
interface ValueType2 {
key3: number,
key4: number,
}
type KeyType = 'value1' | 'value2';
const map: {[key in KeyType]: ValueType1 | ValueType2} = {
'value1': {
key1: '123',
key2: '123'
},
'value2': {
key3: 123,
key4: 123,
}
}
If we want to ensure that when key = 'value1', the type of Value must be ValueType1 and similarly for value2, is this achievable?