Looking to create a type-safe utility function in Typescript 4.0 for comparing properties of two objects, my initial code snippet is below:
export function propertiesMatch<O extends object, T extends O, S extends O>(first: T, second: S, props: (keyof O)[]) {
return props.every(prop => first[prop] === second[prop])
}
Encountering compile error TS2367 with this implementation, which warns that:
This condition will always return 'false' since the types 'T[keyof O]' and 'S[keyof O]' have no overlap.
This error message is puzzling as it seems logical that if both T
and S
extend an object of type O
, they should have all keys of type O
. Any insights on what I might be overlooking here and recommendations for a more robust solution would be greatly appreciated.