I find the following getter function very useful and reusable since it can get a value of the property key of any object with such property.
export function keyOf<a>(value: { key: a; }) : a {
   return value.key;
}
Similarly I can define a universal setter function:
export function withKey<a>(value: { key: a; }, key: a) : void {
    value.key = key;
}
The only problem is that instead of returning void I need it to return the original object value with the modified key property. Something like this:
export function withKey<b, a extends { key: b }>(value: a, key: b) : a {
   value.key = key;
   return value;
}
But this is not a valid piece of TypeScript code.
Question: How else can I get a typesafe universal setter function that returns the original object with its property set?
UPDATE:
In the current TypeScript dependencies between type parameters are prohibited. I believe it was done for making the type system simplier and faster. However this limitation prevents certain useful scenarios like the one in question. There is a hack that can turn a dependency between type parameters into a functional dependency:
export function withKey<a, b>(
    value: a,
    key: b,
    toAssignable: (value: a) => { key: b } = function<c>(anything: c) : c {
        return anything;
    }
) : a {
    toAssignable(value).key = key;
    return value;
}
This looks ugly as hell and alters the original signature, but it compiles and works.
Does anyone know a better way?
 
    