Say I have
interface User {
name: string;
age: number;
}
interface Car {
year: number;
model: string;
}
interface Action<T> {
assign<K extends keyof T>(key: K, value: T[K]): void;
}
This allows me to do:
const userActions: Action<User> = ...;
const carActions: Action<Car> = ...;
userActions.assign('age', 1); // all good
userActions.assign('foo', 2); // error that `foo` does not exist
userActions.assign('age', 'foo'); // error that type string is not assignable to age
carActions.assign(...); // same behavior for car
Now I want to create auxiliary methods that can be passed to assign, for example:
const logAndAssign = (key, value): void;
And I want to be able to do
userActions.assign(logAndAssign('age', 1));
// etc
And so I want these aux methods logAndAssign to get the type passed to them. How can I achieve this?
logAndAssigncall to fill in both arguments to.assign, it would only be able to fill in the first.