I am in the process of creating a simple abstraction that displays patches to an object.
type MyObject = {
attributeA: string;
attributeB: boolean;
attributeC: number;
};
type MyObjectKeys = keyof MyObject;
type Difference<Key extends MyObjectKeys = MyObjectKeys> = {
// The value of this attribute should determine
// the type of the old and new value.
key: Key;
oldValue: MyObject[Key];
newValue: MyObject[Key];
};
type Patch = {
patches: Difference[];
};
const patch: Patch = {
patches: [
{
key: 'attributeB',
// Should be inferred as boolean.
oldValue: '',
// Both should have the same inferred type.
newValue: 9,
},
],
};
I am aiming for oldValue
and newValue
to be typed based on the provided key
.
Regrettably, as mentioned in the code comments, it is not functioning correctly.
I appreciate any guidance!