I'm looking for a solution similar to:
interface Operation<T, K extends keyof T> {
key: keyof T;
operation: 'add' | 'remove';
value: T[K];
}
but without the necessity of passing K
as a template. Essentially, I want to be able to achieve:
interface User {
name: string;
age: number;
}
// this is acceptable
const operation: Operation<User> = {
key: 'name',
operation: 'add',
value: 'the value',
}
// this is acceptable
const operation: Operation<User> = {
key: 'age',
operation: 'add',
value: 123,
}
// this is not valid
const operation: Operation<User> = {
key: 'age',
operation: 'add',
value: '123', // type is wrong, should be number
}
Any suggestions on how I can accomplish this?