I run into this sort of issue fairly frequently, especially when trying to convert generic functions to ts.
In the example below I need to compare objects with different structures but both of which have the same key. For example they both represent the same DB object with the same id key but are types of different fidelity.
// Basic pseudocode of what I want
export const compare = <
Item,
NewItem,
SharedKey extends keyof Item & keyof NewItem,
>(
oldThing: Item,
newThing: NewItem,
idKey: SharedKey,
) => {
return oldThing[idKey] === newThing[idKey];
}
const example1 = {id: 1, name: 'bob'}
const example2 = {id: 1, name: 'bob': age: 20}
compare(example1, example2, 'id') // true
const broken1 = {id: 'axy', name: 'bob'}
compare(broken1, example2, 'id') // this should give type errors as both objects don't have a shared 'id' key with the same type