Assume I have a type
type MyType = {
'KEY1': 'ValueForKey1';
'KEY2': 'ValueForKey2';
'KEY3': 'ValueForKey3';
}
and I have function which takes values from MyType as {key: 'KEY1', value: 'ValueForKey1'}
function myFunc(entry: MyParamType<MyType>) {
}
Where I defined MyParamType as
type MyParamType<T> = {
key: keyof T;
value: T[keyof T]
};
This works fine for
myFunc({
key: 'KEY1',
value: 'ValueForKey1'
})
and fails as expected for
myFunc({
key: 'KEY1',
value: 'ValueForKey5' // should fail
})
The issue I am facing is it can take the value of any key instead of that particular key So
myFunc({
key: 'KEY1',
value: 'ValueForKey2'
})
succeeds instead of failing.
How do I add the conditional restriction that only the corresponding key should succeed?
Playground link.