I would like to write a type-safe utility function for comparison of properties of 2 given objects in Typescript 4.0. My initial attempt is as following:
export function propsAreEqual<O extends object, T extends O, S extends O>(first: T, second: S, props: (keyof O)[]) {
return props.every(prop => first[prop] === second[prop])
}
However, I get a compile error TS2367 with this approach, which states that:
This condition will always return 'false' since the types 'T[keyof O]' and 'S[keyof O]' have no overlap.
That error seems counter-intuitive to me. If both T and S extend object of type O then shouldn't they both necessarily contain all keys of type O? I would greatly appreciate if anyone could clarify what am I missing here and if there is a more sound approach to what I am trying to achieve?