I am trying to compare if an item exists in another array by checking a common property value. For the below code it is throwing the below error:
TS2367: This condition will always return 'false' since the types 'U[K]' and 'T[K]' have no overlap.
const sourceArray = [{requestId: "1", name: "henry"},{requestId: "2", name: "bob"}]
const arrayListToCompareWith = [{requestId: "1", age: 30}, {requestId: "9", age: 40}]
const commonItems = getItemsInSourceArrayWhichArePresentInArrayToBeComparedWith(sourceArray,arrayListToCompareWith, "requestId" )
function getItemsInSourceArrayWhichArePresentInArrayToBeComparedWith<
T,
U,
K extends keyof T & keyof U,
>(
sourceArray: T[],
arrayListToCompareWith: U[],
comparisonKey: K, // "requestId"
) {
const itemsInSourceArrayWhichAreNotPresentInArrayToBeComparedWith: T[] =
sourceArray.filter((sourceArrayItem: T): boolean => {
const { [comparisonKey]: sourceArrayItemComparisonKeyValue } = sourceArrayItem; sourceArrayItemComparisonKeyValue
const itemExistsInTheArrayWeAreComparingWith: boolean =
arrayListToCompareWith.some((itemToCompareWith: U): boolean => {
const comparisonValueInItemToCompareWith = itemToCompareWith[comparisonKey]; // U[K]
const comparisonValueInSource = sourceArrayItemComparisonKeyValue; //T[K]
// TS2367: This condition will always return 'false' since the types 'U[K]' and 'T[K]' have no overlap.
const check = comparisonValueInItemToCompareWith === comparisonValueInSource;
return check;
});
return itemExistsInTheArrayWeAreComparingWith;
});
return itemsInSourceArrayWhichArePresentInArrayToBeComparedWith;
}
Below is the TS playground link for the above code problem:
TS Playground link to run the code
CodePudding user response:
The problem is that you didn't tell Typescript that there is any relationship between T
and U
, in particular you didn't say that they must have the same value type for the property K
.
Actually there is no need for your second type parameter U
- all we need to say is that the second array contains objects which have a key K
with the same value type as T
does. This can be more simply written as Pick<T, K>
.
function filterByKey<T, K extends keyof T>(arr: T[], keysToKeep: Pick<T, K>[], key: K) {
return arr.filter(
item => keysToKeep.some(
otherItem => item[key] === otherItem[key]
)
);
}