In my TypeScript interface, I have defined the following structure:
MyInterface {
'key1': number | string;
'key2': string;
'key3': SomeOtherInterface;
}
I am looking to create a new type that utilizes the properties of MyInterface:
MyType<K = keyof MyInterface> = {
'key': K;
'value': MyInterface[K];
'somethingElse': string
}
My goal is to ensure that if the key in MyType is 'key1', then the value must be of type number | string
, and if the key is 'key2', the value must be of type string
, and so on. However, the approach above results in an error: Type 'K' cannot be used to index type 'MyInterface'. How should I go about defining MyType?